Rxjava Getting Started and using

Source: Internet
Author: User

Meet Rxjava

Rxjava is an implementation of Reactivex on the JVM, Reactivex uses observable sequences to combine asynchronous and event-based programs.
OBSERVABLE (Observer) and Subscriber (subscribers) are the two main classes. On RxJava, a Observable is a class that emits a stream of data or an event, and Subscriber is a class that processes (takes action) the emitted items (data streams or events). A Observable standard stream emits one or more item, then completes successfully or fails. A Observable can have more than one subscribers, and with each item emitted by Observable, the item will be sent to the Subscriber.onnext () method for processing. Once Observable no longer issues items, it will call the Subscriber.oncompleted () method, or Observable will call the Subscriber.onerror () method if there is an error.

The reason why Rxjava has been so hot lately is that it's sequential processing, and that this power comes from its operators. Here, I no longer repeat, online a lot of introduction operator articles, you can see Rxjava Chinese documents, or Rxjava actual combat. Believe that, when you understand, the Rxjava will have a certain way of thinking.

RxJava Getting Started

It is recommended to look at the above two articles about Rxjava introduction, so that you can not read the confused, except the great God.
Contact Rxjava, because a colleague of the company project team used Rxjava to make a callback to the login operation and return to the original interface, based on this demand, Rxjava into the world.
This way, I'll talk about Rxjava. The control of the thread on which the processing resides. This is another reason for Rxjava. --rxjava's schedulers (scheduler) feature.

For any Observable you can define in two different threads, Observable will operate on top of it. Using Observable.observeon () can be defined on a thread that can be used to listen and check the latest items issued from Observable (Subscriber onnext,oncompleted and OnError methods are executed at O Bserveon on the specified thread) and use Observable.subscribeon () to define a thread that runs our Observable code (long-running operations).

RxJava is single-threaded by default, you will need to use the Observeon () and Subscribeon () methods to bring multi-threaded operations to your app. RxJava comes with several ready-made schedulers for observables use, such as Schedulers.io () (for I/O operations), schedulers.computation () (computational work), and Schedulers.newthread () (new thread created for the task).

This way, be especially careful when you implement a subscription in the Activity Interface (Subscription =observable.subscribe ()), be sure to implement a reverse subscription when the interface is destroyed (Subscription.unsubscribe () ) to prevent memory leaks!

Use

Before useless Rxjava, in order to inform the message, from this page to notify the last page data display to make changes, or the page data changes, notify the relevant multiple pages to make changes, we will think of callbacks, or broadcast. However, this is disgusting, specifically how disgusting, you write your own estimates will know. Now with the Rxjava, found that the implementation of a lot simpler, there is extensibility, ah, to operate some!

First , Android Studio builds a test engineering TestApp to implement the required Rxjava dependency information in the Build.gradle build

This allows us to refer to the required Rxjava method in the class file.

next , establish a Rx Refresh event (rxrefreshevent), which is implemented as follows:

    • Getobservable (): Mainly for the outside class can get this observer, to achieve operation monitoring.
    • Publish (t): Used for the viewer to publish messages based on demand, so that the subsribers can implement the operation.

next , create an observer publishing message interface, and a Subscriber accepts the display interface:

In this way, a simple demo is done. When the observer needs to send the changed data through publish (), the Subscriber is callback in the Action-call () method due to the subscription relationship. Well, the data changes are done! The process of publishing the message is actually one-to-many, that is, multiple subscribers can receive messages from the same observer. Just a few lines of code, simple and practical!

Rxjava Getting Started and using

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.