The role of Rxjava
Observer model
- Many to one, multiple observers, one observed, the observer has a high degree of data sensitivity to the observed and can make changes accordingly.
UML class diagrams
- Observerable of the observed object
notifyObservers()
Will traverse each observer in turn and call itupdate()
.
The observer mode uses the scene
The use of Rxjava
- Create observed -3 ways
- with
create
Methods. Note that Subscriber is the Observer
just
methods
from
methods
- with
- Create observer
onNext()
In the traditional observer modelonupdate()
.
- To subscribe to
- call
subscribe
Observables.OnSubscribe by defaultcall
Method to complete event consumption.
Rxjava source
— Go to class Subscriber
- SubscriptionList holds all subscription events for the Subscriber/Observer, and the events will be deleted from the List when the subscription is unsubscribed. – to enter
observable.subscribe(observer)
- Internally, Subscriber is wrapped as SafeSubscriber and implemented
onCompleted()
.onError()
.onNext()
Methods. - The final call
call(subscriber)
Complete the subscription.
Rxjava operator
Map — Mapping/event transformation
- Func1<String, Bitmap> is an interface that converts a String image path to a Bitmap. The Map operator creates a new Observable and then chain-calls it
subscribe
.
flatMap
- Func1
,>
converts String to Observable.
Rxjava thread control
Schedulers
- Schedulers.immediate(): Executes on the current thread without switching threads.
- Schedulers.newthread (): New threads are always enabled to perform operations.
- Schedulers.io(): Performs I/O operations, such as reading and writing files, and network information interaction (commonly used).
- Schedulers.computation(): Performs CPU-intensive calculations.
- AndroidSchedulers. MainThread () : switch the main thread execution (common).
How do I do thread control
subscribeOn()
Observable: specifies the thread in which the observer is subscribed, which is the thread in which Observable.OnSubscribe is activated.observeOn()
: The thread where the event is consumed.
Example of thread control
observeOn(..)
Specifies the thread on which subsequent operations are performed, and can be called multiple times.subscribeOn(..)
Can be inobserveOn(..)
Can be invoked before or after only once.
SubscribeOn method
subscribeOn
andobserverOn
subscribeOn
andobserverOn
Both operate on threads through the Executor pool.