1. Introduction
-
Rxjava, with chain-style calls, ease of use & loose coupling of events and results.
-
Rxjava, which is so popular because it contains so many operators, can achieve almost all functional requirements through elegant and clean chained code.
-
This paper features: graph more words less, simple logic.
-
I was asked a lot of knowledge about Rxjava in the interview, today I offer a different interpretation, I hope you like it.
2. The directory
3. Introduction
4. Principle Analysis
- Rxjava, extended from the Observer pattern.
4.1 Life Examples
-
Through the mobile phone takeout example, analyze the principle of RXJava.
-
Take-out process
- Involved roles and corresponding processes
4.2 Examples combined with Rxjava
- Rxjava, which contains observer, observed, subscription actions & events, corresponds to the following figure:
- Corresponding Rxjava basic usage code:
public void create(a){
Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> e) throws Exception {
e.onNext("Roast chicken, please.");
e.onNext("One portion of French fries.");
e.onNext("A Coke");
// e.onError(new NullPointerException());
e.onComplete();
}
}).subscribe(new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
Log.e(TAG,"OnSubscribe: Successful subscription");
}
@Override
public void onNext(String s) {
Log.e(TAG,"OnNext: Receive event"+s);
// In order: roast chicken, fries, coke
}
@Override
public void onError(Throwable e) {
Log.e(TAG,"OnError: Event exception"+e.toString());
}
@Override
public void onComplete(a) {
Log.e(TAG,"OnComplete: Event completed"); }}); }Copy the code
Observable: sends events sequentially to observers by subscribing. Observer: Receives events in sequence & responds to feedback.
5. The operator
5.1 Creating Operators
-
Function: The observed sends events to the observer.
-
Application:
5.2 Transformation operators
-
Function: The observed processes sequence events into other sequence events (transformations).
-
How it works: Combine the example of ordering takeout (changing dishes).
- Application:
5.3 Combine Operators
-
Function: Combine multiple observations & combine the events they need to send.
-
Application:
5.4 Filter Operators
-
Role: Filter/filter events sent by the observer.
-
Application:
5.5 Conditional operators
-
Function: Determines whether to receive an event sent by an observer by specifying a condition.
-
Application:
5.6 Operators of Other Functions
-
Function: Function expansion is performed when the observed sends an event.
-
Application:
6. Frame design idea
7. To summarize
-
Rxjava is such a handy, simple and elegant chained task framework library, what are you waiting for?
-
Here, the basic completion of rXJava related introduction and use, I hope you like my article.
-
I hope this article will help those who are preparing for interviews and preparing to use Rxjava.
Beginner’s mind for writing technical articles
-
Accumulation of technical knowledge
-
Consolidation of technical knowledge
-
Technical knowledge sharing
-
Technical knowledge exchange