2016-06-06
Rxjava is a frame of fire in the last two years, the core is asynchronous, but what impresses me most is the idea of responsive programming. Recently, I just want to change my project to be realized with Rxjava, so I studied the next. Parabola and Big Head Ghost two Daniel also explained in detail and image, in fact Rxjava in addition to this kind of responsive programming thinking is not very good understanding, operators are also more difficult to understand part. Responsive programming thought is not few words can tell clearly, want to learn the person is not by looking at a few times blog can learn. I am here mainly to talk about the character, by means of decomposition of the way to lead everyone to see what is going on in the end, take the commonly used map for example.
First look at a pseudo-code:
Observable.create (NewObservable.onsubscrible () {//------------Observable One@override PublicObject Call (.... subscriber) {//---------------------- Call One... subscriber.onnext (); //--------------OnNext One... }). Map (NewFunc1 () {//observable is returned after----------------------map operation@override public Object Call (...) { //----------------------------- Call... }). Subscrible (NewSubscriber () {@override Public voidoncompleted () {} @override publicvoidOnError () {} @override publicvoidOnNext () {//-------------------------OnNext...});
I'll dissect the above section of the Rxjava code. To describe the problem step-by-step, we will first simplify the above code as follows, defining the OBSERVABLETEMP variable:
Observabletemp.subscrible (new Subscriber () {...});
Where Observabletemp is:
Observabletemp =Observable.create (NewObservable.onsubscrible () {//------------Observable One@override PublicObject Call (.... subscriber) {//---------------------- Call One... subscriber.onnext (); //--------------OnNext One... }). Map (NewFunc1 () {//observable is returned after----------------------map operation@override public Object Call (...) { //----------------------------- Call...}) ;
First, let's first analyze what Subscrible did.
Hook.onsubscribestart (observable, observable.onsubscribe). Call (subscriber);
This is the core of the entire subscrible approach. Onsubscribestart returns the Observable.onsubscribe, and Observable.onsubscribe is the passed parameter, which is the this object. Therefore, the main functions of the Subcrible method can be simplified to:
Observabletemp. Onsubscribe.call (new Subscriber () {...});
For example, if
Observabletemp =
Observable.create (New observable.onsubscrible () {
@override
public Object Call (... subscriber) {//---------------1-1
Subscriber.onnext ();
}
})
So observabletemp. Onsubscribe.call (new Subscriber () {...}); decomposition is as follows:
Observabletemp. Onsubscrib = Observable.onsubscrible ()
Here's the 1-1 incoming value in the form we just simplified, should be new Subscriber (). Then the whole thing becomes:
New Observable.onsubscrible () {
@override
public Object Call (.... subscriber) {
......
Subscriber.onnext ();
........
}
}.call (new Subscriber () {...});
Call here is called the @override calling method, so here is further decomposition:
public object Call () {
......
New Subscriber () {....}.onnext ();
........
}
Did you see that? In fact, the whole large piece of code just executes the OnNext method in Subscriber.
I believe I see that the subscribe method has been understood here. But what if the observabletemp is unfolded? That's the code that was restored to the beginning, and what did it do? Take a look at the decomposition of the second step
Step two, what did map do?
We already know observabletemp. Onsubscribe.call (new Subscriber () {...}) is actually called Subscriber OnNext method directly through the Onsubscrible.call method in Observabletemp. So what is a call relationship after adding a map? Map literal meaning should be a mapping operation, that is not the literal map? If it is a mapping, who is the mapping between WHO and who?
To make it easier to explain, let's simplify the first piece of code by simplifying it to:
Observabletemp.map (new Func1 () {...}). Subcrible (new Subscriber () {...});
Where Observabletemp is a simple object creation and assignment process:
Observable.create (new observable.onsubscrible () { @override public Object Call (... subscriber) { ... Subscriber.onnext (); // There's no difficulty in understanding it.
The Operation code for map is:
Lift (new operatormap<t, R> (func));
Lift is the core of Rxjava, and almost all observable operations require lift methods. A.lift (Operator op) returns a new observable, onsubscribe in the new observable.
Observabletemp.map (new Func1 () {...}). Subcrible (new Subscriber () {...});
It becomes:
New Observable (new Onsubscribelift () {@override Public object Call (...) { ...}}). Subcrible (new// -------------------------2-2
is not familiar, Yes, is the first step in the simplification of the steps. So here's the key part, what Onsubscribelift's call does, and if we figure this out, then the map will naturally be clear.
There are two main lines of code in Onsubscribelift:
Super T> st = Hook.onlift (operator). Call (O);p Arent.call (ST);
What is the operator,o here, the parent and the St?
Because o here is the Onsubscribelift call method passed in parameters, by 2-2 can be known, O actually represents the new Subscriber ().
Looking at operator again, it can be seen from the code:
Public Final Super extends R> func) { return lift (new operatormap<t, r>(func)); }
The operator here refers to the operator object that has been wrapped by Operatormap: New Operatormap<t, r> (New Func1 ()), and Hook.onlift (operator) Return is operator, then the above two lines of code can be seen as:
Super T> st = (new operatormap<t, r> (new Func1 ())). Call (O);p Arent.call (ST);
Operatormap's call did three things:
New Mapsubscriber<t, r>(o, transformer); O.add (parent); return parent;
The new Subscriber () and new Func1 () (or transformer objects) are encapsulated by Mapsubscriber. The new Subcriber (Mapsubscriber object) is then placed in the subscription list to finally release the subscription object together. The new Subcriber is also returned.
According to the above,
Super T> st = (new operatormap<t, r> (new Func1 ())). Call (O);p Arent.call (ST);
Can be seen as:
Super T> St new mapsubscriber<t, r>(o, transformer);p Arent.call (ST);
The parent is an incoming parameter, which refers to Observabletemp.onsubscribe, which is the observable one in the opening code.
Further two lines of code can be viewed as:
Super T> St new mapsubscriber<t, r>(o, transformer);(new observable.onsubscrible ( ) { //------------Observableone @override public Object Call (... Subscriber) { //----------------------call one ... Subscriber.onnext (); // --------------OnNext One ... }). Call (ST);
The call parameter in the override here is new Mapsubscriber<t, r> (o, transformer); New Mapsubscriber<t, r> (o, transformer). OnNext The main code is:
Public void OnNext (T t) { = mapper.call (t); Actual.onnext (result); }
Mapper is the transformer object, which is new Func1 (), and actual is new Subscriber (). That is: the type of observable one is passed through new Func1 (). The call method is converted to another subscriber, and finally the OnNext method of the new subscriber is called.
So map the whole process is clear:
A.map (B). Subscribe (C) is:
First, through the map method, the data that you want to convert in a is converted by invoking the call method in B, and finally the converted data is processed by the OnNext method in C.
We throw away the oncompleted and OnError methods,
A.map (B). Subscribe (C) <==> C.onnext (B.call (A.onnext ()))
RxJava Map Operation Detailed