Rxjava use

Operator to query the graph

An overview of the

The most important thing in Rxjava is the Rx idea, the so-called Rx idea, which is the idea of responsive programming (the next step changes with the previous step, order -> order -> cook). It is a good combination of chained programming style and asynchrony. (Also known as card programming)

Card programming (chain programming)

Upper observeable, which can add many maps (cards, etc.) and the data format of the latter (input parameters) varies with the previous (return values). (Every added map still returns this–Observable, so it can always “.” Link the top Observables with the bottom observes by subscribing.

use

RxJava + Retrofit completes network requests





View image stabilization

View anti-shake: click multiple times within 1 second, only respond once. RxBinding provides anti-shake functionality.

doOnNext()

Use doOnNext when you need to insert a method that does not affect Observe (map returns values that affect the underlying ones).

Design patterns for Rxjava

Standard observer design pattern

Wechat public account is a typical observer design pattern. Public account = observed, user = observer, follow public account = subscription. So when an official account pushes a message, all observers can receive the message. The observer design pattern is that multiple observers subscribe to an observed, and all observers are notified when the observed changes.

Concrete implementation:

Abstract by observer (public account)

Implemented by the Observed (official account)

Observer abstraction (user)



Observer implementation (user)

The test class

RxJava Observer design pattern

RxJava’s observer design pattern is more appropriately called the publish-subscribe pattern.

Common observer design pattern: observed (one to many) observer. RxJava: Observed (many-to-one) observer.

General observer design Mode:The observed has a container inside (a list of observers) and then iterates through the list to send a message to the observer. RxJava:There is no list inside the observed, there is an intermediate layer (emitter) between the observed and the observer, the observed sends messages to the emitter, and the emitter sends messages to the observer, which decouples the two.

The source code parsing

1. Custom observers

There are only four methods in the observer interface: onSubscribe () – this method is called before executing any code, such as popup Windows. OnComplete and so on.

2. Create an observer

3. To subscribe to

The subscribeActual () method calls which one depends on the layer, in this case observableccreat

Call the emitter’s onNext() method

As can be seen from the above code, the design pattern of Rxjava is to associate the custom Observer with the custom observed (source) by subscription, while the upper Observer will be wrapped by the Observer, and the Observer will call the lower onNext, and the lower onNext will call the lower onNext until the Observer onNext

Hook

Hook mechanism: Hook, interceptor, listener. Before RxJava is executed, there is a global interceptor that can intercept all kinds of things, such as knowing all the maps, create, etc.

So the global custom hook is to call this method

map

Once you add map, it becomes a process of unpacking. When the ObservableMap. Subscribe (endpoint) subscription method is called, the subscribeActual(Observer) is called and the observer is the endpoint. At the beginning of the subscription, the endpoint is passed to the ObservableMap, and the entire ObservableMap is then packaged into a package that is passed to the upper layer. The upper layer map then wraps the lower layer ObservableMap (ObservableMap (endpoint)), all the way to the top layer. The so-called Onion model.

Call the relationship, package the end points up layer by layer from the start of the subscription, and then unpack them down layer by layer when you call onNext

Packet unpacking

Decoration model

Chain calls, create returns Observable, and the underlying map returns Observable. The layer upon layer decoration is called the decoration model.

conclusion

The design pattern of Rxjava is to call the decoration in a chain from top to bottom through the decoration model, and then wrap it layer by layer from the trigger point of the subscription, from the end point, and unwrap it layer by layer when calling OnNext.

thread

Thread switching is just two, where does the subscribeOn subscriber run, and where does the observeOn observer run

Schedulers.io()

Schedulers.io() is used for long I/OS and Schedulers.computation() is used for frequent calculations



Go straight to the bottom



Go straight to the bottom



Go straight to the bottom



Schedulers.io() so this method declares a thread pool for core thread 1

.subscribeon () assigns XXX threads to the above

So if you don’t write observeOn, you’re putting all of your code in the IO () thread, because you’re throwing source.subscribe(parent) and the end of the package in the new Runnable

AndroidSchedulers.mainThread()

Static final Scheduler defined as schedulers.io ().

New Handler(looper.getMainLooper ()) is the Handler that we use. This is the standard way to write it. We pass in the main thread’s Looper and get the main thread’s Handler Send messages to the main thread through its Handler

.observeon () allocates XXX threads to the following



Go straight to the bottom



new ObserveOnObserver



OnNext, etc., sends a message to the main thread via handle. sendMessage to execute on the main thread