publish, connect, refCount
and share
. They are used together in various combinations. It is very important to understand the difference between:publish().connect()
publish().refcount()
(or just share()
)UITextField
. let url = URL(string: "https://habrahabr.ru/")! let requestObservable = URLSession.shared .rx.data(request: URLRequest(url: url)) requestObservable.subscribe(onNext: { print($0) }) requestObservable.subscribe(onNext: { print($0) })
share()
as salvationshare()
operator to our Observable. let url = URL(string: "https://habrahabr.ru/")! let requestObservable = URLSession.shared .rx.data(request: URLRequest(url: url)) .share() requestObservable.subscribe(onNext: { print($0) }) requestObservable.subscribe(onNext: { print($0) })
share()
is just a wrapper over publish().refcount()
.publish()
, what refcount()
?publish()
and his friend connect()
Connectable Observable is similar to regular Observable, except for one thing. It begins to produce elements not when it is subscribed, but only when the connect()
operator is called on it .
let myObservable = Observable.just(1).publish() print("Subscribing") myObservable.subscribe(onNext: { print("first = \($0)") }) myObservable.subscribe(onNext: { print("second = \($0)") }) DispatchQueue.main.asyncAfter(deadline: .now() + 3) { print("Calling connect after 3 seconds") myObservable.connect() } /* Output: Subscribing Calling connect after 3 seconds first = 1 second = 1 */
myObservable
immediately after it has been created. But they only work after 3 seconds, when the connect()
operator was called. Simply put, connect()
activates the Connectable Observable and includes subscribers. let myObservable = Observable<Int> .interval(1, scheduler: MainScheduler.instance) .publish() myObservable.connect() print("Starting at 0 seconds") let mySubscription = myObservable.subscribe(onNext: { print("Next: \($0)") }) DispatchQueue.main.asyncAfter(deadline: .now() + 3) { print("Disposing at 3 seconds") mySubscription.dispose() } DispatchQueue.main.asyncAfter(deadline: .now() + 6) { print("Subscribing again at 6 seconds") myObservable.subscribe(onNext: { print("Next: \($0)") }) } // Output: /* Starting at 0 seconds Next: 0 Next: 1 Next: 2 Disposing at 3 seconds Subscribing again at 6 seconds Next: 6 Next: 7 Next: 8 Next: 9 ... */
connect()
method returns Disposable
. Thus, you can stop the production of elements by calling the dispose()
method of this Disposable
, or you can provide this DisposeBag
' DisposeBag
.publish().refcount()
.publish().connect()
and publish().refcount()
refcount()
statement as magic, which is processed for you by unsubscribing the Observers. refcount()
calls connect()
automatically when the first Observer is signed, so there is no need to do it yourself. let myObservable = Observable<Int> .interval(1, scheduler: MainScheduler.instance) .publish() .refCount() print("Starting at 0 seconds") let mySubscription = myObservable.subscribe(onNext: { print("Next: \($0)") }) DispatchQueue.main.asyncAfter(deadline: .now() + 3) { print("Disposing at 3 seconds") mySubscription.dispose() } DispatchQueue.main.asyncAfter(deadline: .now() + 6) { print("Subscribing again at 6 seconds") myObservable.subscribe(onNext: { print("Next: \($0)") }) } // Output: /* Starting at 0 seconds Next: 0 Next: 1 Next: 2 Disposing at 3 seconds Subscribing again at 6 seconds Next: 0 Next: 1 Next: 2 Next: 3 ... */
publish().connect()
and publish().refcount()
(or share()
) control the unsubscribe mechanism from Obervables differently.publish().connect()
, you need to manually control the resource cleanup mechanism of your Observable (described in the note under the spoiler) . Your sequence behaves as active and produces items all the time, regardless of subscriptions.publish().refcount()/share()
keeps track of how many Observers subscribe to the Observable and do not disable the former from the latter as long as at least one subscriber exists. In other words, when the subscriber count drops to zero, the Observable “dies” and stops producing any items.Source: https://habr.com/ru/post/336662/
All Articles