reference

RxSwift Chinese document

Github RxSwift

1. AsyncSubject

The AsyncSubject emits the last element (and only the last element) after the source Observable emits a completion event, or only a completion event if the source Observable emits no elements. So AsyncSubject only has one completion event.

It emits the final element to subsequent observers. If the source Observable aborts because it generates an error event, the AsyncSubject emits no elements and sends the error event.

/* AsyncSubject emits the last element (and only the last element) after the source Observable emits a complete event. If the source Observable emits no elements, only a complete event. So AsyncSubject only has one completion event. It emits the final element to subsequent observers. If the source Observable aborts because it generates an error event, the AsyncSubject emits no elements and sends the error event. */ func setupAsyncSubject() { let subject = AsyncSubject<String>() subject .subscribe { print("AsyncSubject 1 Event :",$0) } .disposed(by: OnNext ("🐶") subject.onNext("🐥") subject.onnext ("🐍") subject.onnext ("🐯") subject.oncompleted ()}Copy the code

Output:

AsyncSubject 1 Event: Next (🐯) AsyncSubject 1 Event: completedCopy the code

2. PublishSubject

The PublishSubject will send elements generated after the subscription to the observer, and elements issued before the subscription will not be sent to the observer. If you want the observer to receive all elements, you can create an Observable by using the Create method of an Observable, or by using ReplaySubject.

If the source Observable aborts because it generates an error event, the PublishSubject does not emit any elements, but sends the error event.

/* PublishSubject will send elements generated after the subscription to the observer, and elements issued before the subscription will not be sent to the observer. If you want the observer to receive all elements, you can create an Observable by using the Create method of an Observable, or by using ReplaySubject. If the source Observable aborts because it generates an error event, the PublishSubject does not emit any elements, but sends the error event. */ func setupPublishSubject() { let subject = PublishSubject<String>() subject .subscribe {print("PublishSubject 1 Event: ", $0)} .disposed(by: DisposeBag) subject.onnext ("🐶") subject.onnext ("🐥") subject.subscribe {print("PublishSubject 2 Event: ", $0)}. Prompt (by: disposeBag) subject.onnext ("🐍") subject.onnext ("🐯") subject.oncompleted ()}Copy the code

Output:

PublishSubject 1 Event: Next (🐶) PublishSubject 1 Event: Next (🐥) PublishSubject 1 Event: Next (🐍) PublishSubject 2 Event: Next (🐍) PublishSubject 1 Event: Next (🐯) PublishSubject 2 Event: Next (🐯) PublishSubject 1 Event: completed PublishSubject 2 Event: completedCopy the code

3. ReplaySubject

The ReplaySubject will send all elements to the observer, regardless of when the observer subscribed.

There are several versions of the ReplaySubject, some of which will only send the latest n elements to the observer, and some of which will only send the latest elements to the observer within a limited period of time.

If you use ReplaySubject as an observer, be careful not to call onNext, onError, or onCompleted on multiple threads. This leads to disorderly calls, which can have unexpected results.

/* ReplaySubject will send all elements to the observer, regardless of when the observer subscribed. There are several versions of the ReplaySubject, some of which will only send the latest n elements to the observer, and some of which will only send the latest elements to the observer within a limited period of time. If you use ReplaySubject as an observer, be careful not to call onNext, onError, or onCompleted on multiple threads. This leads to disorderly calls, which can have unexpected results. */ func setupReplaySubject() { /// Creates new instance of `ReplaySubject` that replays at most `bufferSize` last elements of sequence. /// /// - parameter bufferSize: Maximal number of elements to replay to observer after subscription. /// - returns: New instance of replay subject. let subject = ReplaySubject<String>.create(bufferSize: 1) subject .subscribe{print("ReplaySubject 1 Event: ", $0)} .disposed(by: DisposeBag) subjection.onnext ("🐶") subjection.onnext ("🐥") subjection.subscribe {print("ReplaySubject 2 Event: ", $0)}. The disposed (by: disposeBag) subject. OnNext (" 🐍 ") subject. OnNext (" 🐯 ")}Copy the code

Output:

ReplaySubject 1 Event: Next (🐶) ReplaySubject 1 Event: Next (🐥) ReplaySubject 2 Event: Next (🐥) ReplaySubject 1 Event: Next (🐍) ReplaySubject 2 Event: Next (🐍) ReplaySubject 1 Event: Next (🐯) ReplaySubject 2 Event: Next (🐯)Copy the code

4. BehaviorSubject

/* When the observer subscrires to the BehaviorSubject, it emits the latest element in the source Observable (default element if none exists). The subsequent elements are then sent out. If the source Observable aborts because it generated an error event, the BehaviorSubject doesn't emit any elements, but instead emits the error event. */ func setupBehavioreSubject() { let subject = BehaviorSubject(value: "🏀") subject. subscribe{print("BehaviorSubject 1 Event: ", $0)}. Prompt (by: OnNext ("🐶") subject.onNext("🐥") subject.subscribe {print("BehaviorSubject 2 Event: ", $0)} .disposed(by: OnNext ("🅰️") subject.onNext("🅱️") subject. subscribe{print("BehaviorSubject 3 Event: ", $0)}. The disposed (by: disposeBag) subject. OnNext (" 🍎 ") subject. OnNext (" 🍐 ")}Copy the code

Output:

BehaviorSubject 1 Event: Next (🏀) BehaviorSubject 1 Event: Next (🐶) BehaviorSubject 1 Event: Next (🐥) BehaviorSubject 2 Event: Next (🐥) BehaviorSubject 1 Event: Next (🅰️) BehaviorSubject 2 Event: Next (🅰️) BehaviorSubject 1 Event: Next (🅱️) BehaviorSubject 2 Event: Next (️ ️) BehaviorSubject 3 Event: Next (🅱️) BehaviorSubject 1 Event: Next (🍎) BehaviorSubject 2 Event: Next (🍎) BehaviorSubject 3 Event: Next (🍎) BehaviorSubject 1 Event: Next (🍐) BehaviorSubject 2 Event: Next (🍐) BehaviorSubject 3 Event: Next (🍐)Copy the code