RxJava && Agera Brief analysis of basic call flow from source (1)

Source: Internet
Author: User

Copyright notice: This article by Jinzhong Hope original article, reproduced please indicate the source:
Article original link: https://www.qcloud.com/community/article/123

Source: Tengyun https://www.qcloud.com/community

Believe that a lot of Android or Java research and development students should have heard of Rxjava, especially in the Android development circle, Rxjava gradually began to become popular. There are also many students who have started to use Rxjava in their own projects. It can help us to eliminate complex and cumbersome code when dealing with asynchronous events, especially when callbacks are embedded in callbacks in some scenario logic, and using Rxjava still keeps our code very readable and concise. In addition, the asynchronous data flow concept of the programming model is also widely used in the mobile terminal, including network calls, user touch input and system frame, such as a variety of response-driven scenarios. So now, let's analyze the Rxjava response process.
(This article is based on RxJava-1.1.3)

I. Usage

Let's start with a simple example:

The result of the operation is:

It is not difficult to see the overall invocation process from the results:

First, by invoking the Observable.create() method to generate an observer, and then here we call the map() method to the original observer of the Data Flow transformation operations, the generation of a new observer (why is the new observer after the text will speak), and finally call the subscribe() method, passed to our observer, Here the Observer subscribes to the new observer generated after the call to map ().

Throughout the process we will notice three protagonists: Observable, Onsubscribe, subscriber, all of which are done around them. It is not difficult to see the division of the three characters here:

    • Observable: The source of the person being observed, or the observer itself
    • Onsubscribe: Used to notify the observer of different behavior
    • Subscriber: Observers, by implementing a corresponding method to produce specific processing.

So then we will analyze the specific process with these three roles as the center.

Two. Analysis 1. Subscription process

First we enter to Observable.create() look at:

Here the constructor generates a observable object and assigns the incoming onsubscribe to its own member variable onsubscribe , and so on, where does the hook come from? We look up:

RxJavaObservableExecutionHookThis abstract proxy class does not do any processing on the Onsubscribe object by default, but by inheriting the class and overriding it, onCreate() we can do some extra work on the timing of these methods, such as playing log or some data collection.

Now that the most initial observer has been generated, let's take a look at the observer side. We know observable.subscribe() how to implement a subscription relationship between the Observer and the observer by invoking the method to pass in an observer. Look at the code:

Here we omit some irrelevant code to see the main part, the subscribe.onStart() default NULL implementation We do not care about it, for the incoming subscriber to be packaged SafeSubscriber , this SafeSubscriber to the original subscriber series of methods to do a more perfect treatment, including: onError() with onCompleted() Only one of them will be executed, and onError() if onCompleted() it is executed, it will no longer be able to onNext() hold the case again. This is encapsulated as SafeSubscriber after, called onSubscribe.call() , and Subscriber is passed in, so that a subscription is completed.

Obviously, subscriber as an observer, after the completion of the subscription behavior, its specific behavior in the entire chain call plays a vital role, we look at its internal composition of the main part:


Each subscriber holds one SubscriptionList , and this list holds all subscription events for that observer, while subscriber also implements the Subscription interface, and when the Subscriber cancels the subscription, it will hold all of the event list Subscriptioncancels the subscription and no longer accepts any subscription events. At the same time, by limiting the amount of traffic that the Producer Subscriber receives, this limit is actually added to the Subscriber.onNext() method, and is onComplete() onError() not affected by it. Because it is the underlying abstract class, and onNext() onComplete() unification is onError() not handled here.

2. Transformation process

We may be able to process the data stream before we receive the observable message, such as map (), FlatMap (), debounce (), buffer (), and so on, in this case we use the map () method, It receives the data emitted by the original observer and emits the results returned by the method as new data, equivalent to a layer of intermediate conversions:

Let's look at the transformation process:

Here is achieved through a lift() method, and then see the other conversion methods found inside also use lift (), it seems that this lift() is the key, but not urgent, we first look at this OperationMap is what:

Operationmap implements the method of the operator interface, call() which accepts the external observer and constructs a new observer as a parameter, which is not difficult to find o.onNext(transformer.call(t)) ; This sentence plays a vital role, the interface transformer here Convert generic T to generic r:

After that, the converted data is passed back to the original observer's OnNext () method, and the conversion of the observed data stream is completed, but you should also notice that the new observer we used to do the conversion does not implement the operation of the subscribed observer, and where is this subscription operation implemented? The answer is the following lift() :

Here we are reborn as an Observable object, in the call () method of the member of this new object, onSubscribe we pass the operator.call() previously generated observer St of the non-generated subscription, and then pass it as a parameter to the beginning onSubscribe.call() , that is, the process of completing the intermediate subscription.
Now we'll comb through the whole process:

    • One map () transform

    • Generate a new subscriber based on the operator instance

    • Generate a new observable by lift ()

    • Original Subscriber subscribe to new observavble

    • New observable Onsubscribe notify new Subscriber subscribe to original observable

    • The new Subscriber passes the message to the original subscriber.

For ease of understanding, here's a picture of the drop line:

This is map() the process of a transformation, in fact, many times it map() is the same reason: the outermost target subscriber after the subscription behavior, onSubscribe.onNext() will be nested layer by level call, until the initial observable by the lowest subscriber subscription, The message is transmitted to the target subscriber through a layer of changes in the operator. Again, the plot to throw the line:

As for the other changes of the implementation process is also very similar, with the help of different implementations of operator to achieve the purpose of transforming the data stream. For example, F latMap() , which needs to be performed two times lift() , where the second is Operationmerge, will be converted to each of the observable data stream through InnerSubscriber this link subscription, in the InnerSubscriber的onNext() get R, and then through the incoming parent (that is, the original MergeSubscriberThey are all emitted (emit) out, by the outermost of our incoming Subscriber unified reception, so that T => Observable<R> => R the conversion is completed:




In addition, there are a number of different operators, and if they do not meet your needs, you can also customize the new operators by implementing the operator interface. Flexible use of them can often achieve a multiplier effect, such as through the use sample() , debounce() such as backpressure the need to effectively avoid the operator, and so on, here is not introduced.

The next section will continue to start profiling from the thread switching process

Article source public number: QQ Space terminal Development team (QZONEMOBILEDEV)

RxJava && Agera Brief analysis of basic call flow from source (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.