Some of the things we encounter are very special. They are both a listening sequence and an observer.

For example, the current text of textField. It can be viewed as a sequence of text generated by user input. It can also be an external text sequence to control the viewer of the current display:

Subscribe (onNext: {text in show(text: text)}); // Let Observable = textfield.rx.text Observable. Subscribe (onNext: {text in show(text: text)})Copy the code
// Let observer = textfield.rx. text Let text: Observable<String? > =... text.bind(to: observer)Copy the code

This feature exists in many UI controls, such as switch on/off state, segmentedControl selected index number, datePicker selected date, and so on.

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.


demo

let disposeBag = DisposeBag() let subject = AsyncSubject<String>() subject .subscribe { print("Subscription: 1 Event:", $0) } .disposed(by: DisposeBag) subject.onnext ("🐶") subject.onnext ("🐱") subject.onnext ("🐹") subject.oncompleted ()Copy the code

Output result:

Subscription: 1 Event: Next (🐹) Subscription: 1 Event: completedCopy the code

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.


demo

let disposeBag = DisposeBag() let subject = PublishSubject<String>() subject .subscribe { print("Subscription: 1 Event:", $0) } .disposed(by: DisposeBag) subject.onnext ("🐶") subject.onnext ("🐱") subject.subscribe {print("Subscription: 2 Event: ", $0)}. The disposed (by: disposeBag) subject. OnNext (" 🅰 ️ ") subject. OnNext (" 🅱 ️ ")Copy the code

Output result:

Subscription: 1 Event: Next (🐶) Subscription: 1 Event: Next (🐱) Subscription: 1 Event: Next (🅰️) Subscription: 2 Event: 2 Next (🅰️) Subscription: 1 Event: Next (🅱️) Subscription: 2 Event: Next (️)Copy the code

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.


demo

let disposeBag = DisposeBag() let subject = ReplaySubject<String>.create(bufferSize: 1) subject .subscribe { print("Subscription: 1 Event:", $0) } .disposed(by: DisposeBag) subject.onnext ("🐶") subject.onnext ("🐱") subject.subscribe {print("Subscription: 2 Event: ", $0)}. The disposed (by: disposeBag) subject. OnNext (" 🅰 ️ ") subject. OnNext (" 🅱 ️ ")Copy the code

Output result:

Subscription: 1 Event: Next (🐶) Subscription: 1 Event: Next (🐱) Subscription: 2 Event: Next (🐱) Subscription: 1 Event: Next (🅰️) Subscription: 2 Event: Next (🅰️) Subscription: 1 Event: Next (️) Subscription: 2 Event: Next (async)Copy the code

BehaviorSubject

When the observer subscribles 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.


demo

Let disposeBag = disposeBag () let subject = BehaviorSubject(value: "🔴") subject.subscribe {print("Subscription: 1 Event:", $0) } .disposed(by: DisposeBag) subject.onnext ("🐶") subject.onnext ("🐱") subject.subscribe {print("Subscription: 2 Event:", $0) } .disposed(by: OnNext ("🅰️") subject.onNext("🅱️") subject. subscribe {print("Subscription: Prompt (by: disposeBag) subject.onnext ("🍐") subject.onnext ("🍊")Copy the code

Output result:

Subscription: 1 Event: Next (🔴) Subscription: 1 Event: Next (🐶) Subscription: 1 Event: Next (🐱) Subscription: 2 Event: Next (🐱) Subscription: 1 Event: Next (🅰️) Subscription: 2 Event: Next (🅰 disk) Subscription: 1 Event: Next (🅱️) Subscription: 2 Event: Next (🅱️) Subscription: 3 Event: Next (️) Subscription: 1 Event: Next (🍐) Subscription: 2 Event: Next (🍐) Subscription: 3 Event: Next (🍐) Subscription: 1 Event: Next (🍊) Subscription: 2 Event: Next (🍊) Subscription: 3 Event: Next (🍊)Copy the code

ControlProperty

ControlProperty is specifically used to describe UI control properties, which have the following characteristics:

  • Does not produceerrorThe event
  • Must be inMainSchedulerSubscription (mainline subscription)
  • Must be inMainSchedulerListening (main thread listening)
  • Shared add-on

Green mountains do not change, green water flow, see you soon, thank you for your support!