RxJava is a series of operations implemented using the observer pattern, so it's important to understand the observer, the observed, and the concepts of subscription and events in the observer pattern.
Observable: In the observer pattern, it is referred to as the "observed";
Observer: The "observer" in the observer pattern, which can receive data sent by the Observable;
subscribe: Subscription, where the observer subscribes to the observed through the Observable's subscribe() method;
Subscriber: Also a type of observer, in version 2.0 it is not substantially different from Observer, except that Subscriber is used in conjunction with Flowable (also an observed), where Observer is used to subscribe to Observable, and Subscriber is used to subscribe to Flowable.
Observer Pattern#
The implementation of rxjava is mainly through the observer pattern.
Object A (the observer) is highly sensitive to certain changes in Object B (the observed) and needs to react the moment B changes.
In the observer pattern of the program, the observer does not need to constantly monitor the observed; instead, it uses a registration or subscription method to inform the observed: I need your certain state, and you should notify me when it changes.
At the same time, we can also have multiple observers corresponding to one observed.
In fact, there are many built-in observer patterns in Android. The most obvious is the click event. For example, when you click a button, a Toast pops up. So, when we click the button, we inform the system that we need to show a toast at this moment. Then it pops up. Now, the question arises: Do I need to listen to this button in real-time? The answer is no. This is different from the previous example. In other words, I only need to listen when this button is clicked. This operation is called subscription. That is, the Button subscribes to the OnClickListener through setOnClickListener to listen for the onClick method.
Basic Usage#
The basic implementation of rxjava mainly consists of three points:
- Initialize Observable (the observed)
- Initialize Observer (the observer)
- Establish a subscription relationship between the two
Create Observable#
Create Observer#
- onSubscribe: It is called before any events are sent and can be used for some preparation operations. The Disposable inside is used to sever the upstream and downstream relationship.
- onNext: Ordinary events. Adds the events to be processed to the queue.
- onError: Exception in the event queue, this method will be called when an exception occurs during event processing. The queue will also terminate, meaning no events will be emitted.
- onComplete: Event queue completion. Rxjava not only processes each event individually but also treats them as a queue. When there are no more onNext events emitted, the onComplete method needs to be triggered as a completion indicator.
Create Subscription#
observable.subscribe(observer);
Result#
First, onSubscribe is called, then onNext is executed, and finally it ends with onComplete:
Thread Scheduling#
- subscribeOn() specifies the thread that emits events, while observerOn specifies the thread that the subscriber receives events on.
- If the event-emitting thread is specified multiple times, only the first specification is effective, meaning that multiple calls to subscribeOn() will only consider the first one, and the rest will be ignored.
- However, it is possible to specify the subscriber's receiving thread multiple times, meaning that each time observerOn() is called, the downstream thread will switch once.
Rxjava has built-in several threads for us to choose from:
- Schedulers.io() represents the thread for IO operations, typically used for network, file read/write, and other IO-intensive operations.
- Schedulers.computation() represents CPU computation-intensive operations, such as operations that require a lot of calculations.
- Schedulers.newThread() represents a conventional new thread.
- AndroidSchedulers.mainThread() represents the main thread of Android.