RxJava--Concise asynchronous operation (i)

Source: Internet
Author: User

As more and more people begin to mention RxJava, given the current hot and mysterious status of RxJava, write this article to RxJava a relatively detailed introduction to Android developers.

The purpose of this article is two: 1. For people interested in RxJava some introductory guidelines 2. Some more in-depth analysis of people who are using RxJava but still have doubts.

Before the text begins, put the GitHub link and introduce the dependent gradle code: Github:
Https://github.com/ReactiveX/RxJava
Https://github.com/ReactiveX/RxAndroid
Introduce dependencies:
compile ‘io.reactivex:rxjava:1.0.14‘
compile ‘io.reactivex:rxandroid:1.0.1‘

What the hell is RxJava?

One word: async .

RxJava's self-introduction on the GitHub homepage is "A library for composing asynchronous and event-based programs using observable sequences for th e java VM "(a library that uses observable sequences on a Java VM to compose an asynchronous, event-based program). This is RxJava, which is a very precise generalization.

However, for beginners, this is too ugly to understand. Because it is a "summary", and beginners need an "introduction."

In fact, the essence of RxJava can be compressed into the word async. On the root, it is a library that implements asynchronous operations, and other attributes are based on this.

RxJava fortunately

In other words, "the same is done asynchronously, why do people use it instead of ready-made asynctask/handler/xxx/...?" 』

One word: concise .

The key point of asynchronous operations is the simplicity of the program, because asynchronous code is often difficult to write and readable when the scheduling process is complicated. Android created AsyncTask and Handler , in fact, is to make the asynchronous code more concise. The advantage of RxJava is simplicity, but its simplicity is unique in that it remains concise as the program logic becomes more complex.

Introduction and principle of API 1. Concept: Extended Observer pattern

The asynchronous implementation of RxJava is achieved through an extended observer pattern.

RxJava's Observer pattern

RxJava has four basic concepts: Observable (observable, i.e. the observer), ( Observer observer), subscribe (subscription), event. Observableand Observer by subscribe() means of the subscription relationship, so that Observable you can issue events when needed to notify Observer .

Unlike the traditional observer pattern, the RxJava event callback method defines two special events, in addition to the normal event onNext() (equivalent onClick() / onEvent() ): onCompleted() and onError() .

    • onCompleted(): Event queue end. RxJava not only handles each event separately, but also considers them as a queue. RxJava requires that the onNext() triggering method be used as a flag when no new issues are issued onCompleted() .

    • onError(): Event Queue exception. When an exception occurs during the event processing, it onError() is triggered, the queue is automatically terminated, and no more events are allowed to be emitted.

    • In a correctly-run sequence of events, onCompleted() and onError() there is only one, and is the last in the sequence of events. It is important to note that the onCompleted() onError() two are also mutually exclusive, that is, one is called in the queue, and the other should not be called again.

2. Basic implementation

Based on the above concepts, the basic implementation of RxJava has three main points:

1) Create Observer

Observer is the Observer, which determines what behavior will occur when the event is triggered. How to implement the interface in RxJava Observer :


In addition Observer to the interface, RxJava also has an implemented Observer abstract class: Subscriber . Subscriber Observer There are some extensions to the interface, but their basic usage is exactly the same:

2) Create Observable

Observable is the Observer, which determines when events are triggered and what events are triggered. RxJava uses create() the method to create a Observable and defines an event-triggering rule for it:

As you can see, an object is passed in OnSubscribe as a parameter. is OnSubscribe stored in the returned Observable object, it acts as a schedule, and when it Observable is subscribed, OnSubscribe the method is call() automatically called, and the sequence of events is triggered sequentially according to the setting (for the above code, the Observer Subscriber will be called three times onNext() and one time onCompleted() ). Thus, by invoking the Observer's callback method by the observer, it implements the event passed by the observer to the observer, the Observer pattern.

3) Subscribe (subscription)

In addition to subscribe(Observer) and and support for incomplete subscribe(Subscriber) subscribe() defined callbacks, RxJava is automatically created by definition Subscriber . The form is as follows:

Simply explain what happens in this code.Action1AndAction0Action0is an interface of RxJava, it has only one methodcall(), this method has no parameter and no return value;onCompleted()The method also has no parameter and no return value, soAction0Can be treated as a wrapper object that willonCompleted()The content is packaged to pass in as a parameter.subscribe()To implement a callback that is not fully defined. This can actually be seen as aonCompleted()Method is passed as a parameter into thesubscribe(), which is equivalent to "closures" in some other languages.Action1is also an interface, it also has only one methodcall(T param), this method also has no return value, but has a parameter;Action0Similarly, becauseonNext(T obj)AndonError(Throwable error)is also a single parameter with no return value, soAction1can addonNext(obj)AndonError(error)Package up incomingsubscribe()To implement a callback that is not fully defined. In fact, althoughAction0AndAction1Most widely used in the API, but RxJava is provided with multipleActionXinterface of the form (for example,Action2,Action3), they can be used to wrap different methods without return values.

4) Scenario Example

Here are two examples of

1.

2.

Follow the public number for more information:

RxJava--Concise asynchronous operation (i)

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.