This is the fifth day of my participation in the August Wen Challenge.More challenges in August

preface

In the previous sections, I focused on observables.

I analyzed the sequence from the perspective of code and industrial control, comparing the sequence to the assembly line, which I personally think is a vivid example and plays a very important role in my understanding of Rx.

As for the Observer mentioned this time, I will continue to explain and explain it from the perspective of industrial control.

What is an Observer?

If the sequence is an assembly line, the core function of the assembly line is to transport items, from the point of view of industrial control, this is called output.

A sequence can output an endless stream of items through its defined constructor functions, and as long as there are items to output, there are places to receive those items.

A place that receives these objects is what we might actually call an Observer.

Understand the Observer

In the previous article, digufriend joked that the names of Observable and Observer of Rx are confusing and not easy to understand.

In fact, I think this is difficult to understand because of cultural and mother tongue differences, just like in Chinese, the same thing can be explained by two different styles.

The big guys who write the RxSwift framework usually start with English semantics, so it’s a bit lenient.

But if we look at it in terms of output and input, it’s a lot easier.

An Rx event must start from the event source, that is, it must generate an Observable (sequence) and output it as an output.

The Observer receives the sequence as its own input.

Here are a few examples from RxSwift’s Chinese documentation:

  • When an episode of One Piece is updated, we watch it immediately

    To watch this episode is Observer

    .

The sequence doesn’t do things by itself, it’s just true to its duty to pass events on from one event to another, and it’s the observer who decides what to do and how to do it.

If the sequence is the pipeline, then the observer is the sensor:

Transducer (English name: transducer/sensor) is a detecting device that can sense the measured information and transform it into electrical signal or other required information output according to certain rules to meet the requirements of information transmission, processing, storage, display, recording and control.

Use an Observer

In general, the implementation we subscribe to at the end of the sequence is already an Observer.

The following code, a gesture click event, and the implementation of the click run effect.

We can fully understand it as:

Gesture click (output) ->subscribe(input, implement print method)

Let the tap = UITapGestureRecognizer () the addGestureRecognizer (tap) tap. Rx. Event. The subscribe {_ in print (" tap click event ") }.disposed(by: rx.disposeBag)Copy the code

The above code could have been written like this:

/ / / initializes the click event and add gestures to view the let the tap = UITapGestureRecognizer () the addGestureRecognizer (tap) / / / observer let observer: AnyObserver<UITapGestureRecognizer> = AnyObserver {(_) in print("tap click event ")} /// tap click as output tap.rx.event // bind to observer .subscribe(observer) .disposed(by: rx.disposeBag)Copy the code

Although I will use the first method for writing and readability, the second method is more appropriate for understanding this section.

AnyObserver and Binder

Basically, two different types of observers.

AnyObserver is used when you don’t know what to use as an observer.

You just have to remember these things about Binder:

  • Error events are not handled
  • Ensure that bindings are executed on the given Scheduler (default MainScheduler)

Once an error event occurs, fatalError is executed in the debug environment and error messages are printed in the release environment.

AnyObserver can be used for a variety of purposes, whereas binders generally work with BINDING UI components.

conclusion

Observables This section focuses on the Observer corresponding to an Observable.

The invocation is illustrated by referring to the Observer as an industrial sensor with outputs and inputs.

The Observer has two options, AnyObserver and Binder, note their usage scenarios.

Next video

From my personal reading of the code, Binder is the most used Observer in iOS. Many parameters in the source code of RxCocoa are bound by its properties.

A big part of the reason is that some scenarios are so special that they are both a listening sequence and an observer.

That’s what we’ll do in the next section.

Reference documentation

An Observer

The WanAndroid client written by RxSwift is now open source

Currently RxSwift written wanAndroid client has been open source –Project link.

I took a round of CodeReview and added a few small features:

  • Added dark Mode adaptation for iOS

  • Pull-up loading uses more imperceptive features

  • Replace MBProgressHUD with SVProgressHUD

Remember to give a star!

Attached is an effect picture: