Introduction and basic application of Rxjava _android

Source: Internet
Author: User
Tags throwable

Objective

Because this Rxjava content is not very small, and the application scene is very wide, so this article about Rxjava we will update, today come to an introductory Rxjava bar

First knowledge of Rxjava

What is Rx

Many tutorials in explaining Rxjava, come up to introduce what is Rxjava. Let me first say what is Rx,rx or Reactivex, officially defined as:

Rx is a library of functions that allows developers to write asynchronous and event-based programs using observable sequences and LINQ-style query Operators

See this definition I can only hehe, a little popular point says is this:

Rx is a responsive extension of Microsoft. Net. RX provides an easy way to create asynchronous, event-driven programs with an observable sequence.

This is a little bit clearer, at least to see our familiar asynchronous and event-driven, so simply and inaccurately:

Rx is a kind of responsive programming to create an event-based asynchronous program

Note that this definition is inaccurate, but for beginners, there is a basic understanding.

Another point is that RX is actually a programming idea, in many languages can be achieved, such as Rxjava, RXJS, rxphp and so on. And now all we have to say is Rxjava.

What's Rxjava?

Apart, first define:

Rxjava is a kind of responsive programming implemented in the Java language to create event-based asynchronous programs

Someone asked you this is not nonsense, okay then I'm officially defined:

a library that uses an observable sequence on a Java VM to compose an asynchronous, event-based program

Anyway, I just read this sentence when it is also hehe, of course, now some understanding.

In addition to this: asynchronous, it is a library that implements asynchronous operations.

Extended Observer pattern

For the ordinary observer pattern, I will not dwell here. The simple generalization is that the Observer (Observer) needs to respond at a moment of change in the observed (observable).

Both are bound by registration (register) or by subscription (Subscribe).

For the example of a drop-line teacher, I have enriched it as shown in the picture:

The button is the Observer (observable), the Onclicklistener is the Observer (Observer), and the two reach a subscription (Subscribe) relationship through Setonclicklistener, Then when the button produces the OnClick event, it is sent directly to the Onclicklistener, which makes corresponding response processing.

Of course there are other examples, such as the ContentProvider and Contentobserver in the four Android components.

and Rxjava's observer model is similar to this, but there are a few differences:

Observer and observable are through subscribe() to reach a subscription relationship.

There are three kinds of event callbacks in Rxjava: onNext() , onCompleted() , onError() .

If a observerble does not have any observer, then this observable will not issue any event.

One of the 3rd, here to illustrate, in the Rx, in fact, there are two forms of observable: Hot start observable and cold start observable.

The hot start observable will send a message at any time, even if no observer listens to it.

Cold start observable send messages only if you have at least one subscriber

Although this place for beginners is not very different, but to pay attention to, so the 3rd above is actually aimed at the cold start.

In addition, a summary of the callback events for Rxjava:

onNext() : Basic events.

onCompleted(): End of event queue. Rxjava not only handles each event individually, it also sees them as a queue. Rxjava stipulates that the onNext()  triggering method should be used as a token when no new issue is issued onCompleted() .

onError(): Event Queue exception. When an exception is made during event handling, it onError() is triggered, and the queue is terminated automatically, and no more events are allowed.

It is noteworthy that in a properly running sequence of events, onCompleted() and onError() there is only one, and is the last of the sequence of events. If you call one of these in a queue, you should not call another.

Okay, so let's take a picture and compare it:


How to implement Rxjava

About the steps to achieve rxjava, here I will sum up the general summary.

Create Observer

In Java, the thought of creating an object immediately made us want the new one. Yes, we're going to be new here, too. A observer is actually the interface that implements the observer, note that string is the type that receives the parameter:

Create Observer
observer<string> Observer = new observer<string> () {
 @Override public
 Void OnNext (String s) {
  log.i ("OnNext--->", "Item:" + s);
 }

 @Override public
 void oncompleted () {
  log.i ("oncompleted--->", "Finish");
 }

 @Override public
 void OnError (Throwable e) {
  log.i ("OnError--->", e.tostring ());

And of course here's an abstract class that implements the Observer interface: Subscriber, which is almost exactly the same as the Observer interface, just two more ways to see the summary:

onStart(): It will be called at the beginning of subscribe, and the event has not been sent before it can be used to do some preparatory work, such as clearing 0 or resetting the data. This is an optional method, and its implementation is empty by default. It is important to note that if you Chengyu a requirement for a line that is ready to work (such as a dialog box that displays progress, which must be performed on the main thread), onStart() it does not apply because it is always invoked on the thread that occurs in subscribe and cannot specify the thread.

unsubscribe() : for canceling subscriptions. After this method is invoked, Subscriber will no longer receive events. Generally before this method call, you can use to isUnsubscribed() judge the state first. To avoid a memory leak, call in the appropriate place (such as OnPause () onStop (), etc.) as soon as possible when it is no longer in use unsubscribe() .

Although there are two more methods, but the basic implementation is the same as the observer, so for the moment can not consider the difference between the two. However, it should be noted that:

In essence, in the process of Rxjava subscribe, Observer will always be converted into a subscriber before use.

Create observable

Unlike observer, observable is create() created by means of a method. Note that string is the type that sends the parameter:

Create observable
observable observable = observable.create (new observable.onsubscribe<string> () {
 @ Override public
 void Call (subscriber<. Super string> subscriber) {
  subscriber.onnext ("Hello");
  Subscriber.onnext ("World");
  Subscriber.oncompleted ();
 }
);

We do not consider the process of this.

Subscribe to (Subscribe)

Before, we created observable and Observer, and now we need to use subscribe() methods to connect them to form a subscription relationship:

Subscribe to
OBSERVABLE.SUBSCRIBE (Observer);

It is really a bit strange here, why is observable (the observer) subscribed to The Observer (Observer)? Actually, we'd like to think about the click event of the button before:

Button.setonclicklistener (New View.onclicklistener ())

The button is observed, Onclicklistener is an observer, and Setonclicklistener is a subscription. We are surprised to find that the observer is also subscribed to the observer, so it should be a flow API design, and no impact.

The complete code is as follows:

 Create Observer
 observer<string> Observer = new observer<string> () {
  @Override public
  Void OnNext (String s) {
   log.i ("OnNext--->", "Item:" + s);
  }

  @Override public
  void oncompleted () {
   log.i ("oncompleted--->", "Finish");
  }

  @Override public
  void OnError (Throwable e) {
   log.i ("OnError--->", e.tostring ());

 Create observable
 observable observable = observable.create (new observable.onsubscribe<string> () {
  @ Override public
  void Call (subscriber<. Super string> subscriber) {
   subscriber.onnext ("Hello");
   Subscriber.onnext ("World");
   Subscriber.oncompleted ();
  }
 );

 Subscribe to
 OBSERVABLE.SUBSCRIBE (Observer);

The results of the run are as follows: You can see that the string sent in the observable has been received and printed by observer.

Thread Control--scheduler

Well, here's one of the Rxjava.

In Rxjava, the scheduler corresponds to a thread controller, which allows you to specify the threads that each section of code runs.

Rxjava has built a few scheduler, here's a summary:

Schedulers.immediate(): Running directly on the current thread, equivalent to not specifying a thread. This is the default scheduler.

Schedulers.newThread(): Always enable new threads and perform operations on new threads.

Schedulers.io(): The scheduler used by I/O operations (reading and writing files, reading and writing databases, network information interaction, etc.). The behavior pattern is similar to Newthread (), except that the internal implementation of IO () is a thread pool with no upper limit and can reuse idle threads, so in most cases io () is newThread() more efficient. Instead of putting the calculation work in IO (), you can avoid creating unnecessary threads.

Schedulers.computation() : Calculates the scheduler used. This calculation refers to CPU-intensive computing, that is, operations that do not restrict performance by operations such as I/O, such as the calculation of graphics. This scheduler uses a fixed thread pool, the size of which is the CPU kernel number. Do not put I/O operations in computation() , otherwise I/O operation waiting time will waste the CPU.

AndroidSchedulers.mainThread(), an Android dedicated thread that specifies the operation to run on the main thread.

So how do we switch threads? There are two methods available in Rxjava: subscribeOn() and observeOn() , the difference between the two is:

subscribeOn(): Specifies the subscribe() thread that occurs for the subscription, that is, call() the thread that is executing. Or a thread that is called an event generation.

observeOn() : Specifies the thread that the observer is running on, that is, the onNext() executing thread. Or a thread called event consumption.

Specifically implemented as follows:

Change the running thread
Observable.subscribeon (Schedulers.io ());
Observable.observeon (Androidschedulers.mainthread ());

It's really hard to understand, it's okay, let's look at the phenomenon in concrete examples below.

And this principle, will be in the source level after the analysis of the article detailed explanation, now we lay aside.

First Rxjava case

Well, when you've finished all the basics, now we can all write a demo based on Rxjava.

Here we demonstrate with a Rxjava based asynchronous load network picture.

Since the focus is on Rxjava for asynchronous processing, there is no detail about how to get a picture over a network request.

In addition, we use chained calls and log logs for important locations to observe the thread in which the method executes.

First you need to add dependencies, which is nothing to say:

dependencies {
 compile filetree (include: [' *.jar '], dir: ' Libs ')
 testcompile ' junit:junit:4.12 '
 ...
 Compile ' io.reactivex:rxjava:1.1.6 '

}

Then follow the steps to first create the observable by creating, and note that the type of the Send parameter is bitmap:

Create the Observer
Observable.create (new observable.onsubscribe<bitmap> () {
 /**
 * Replication Call method
 * * PARAM Subscriber Observer Object
 * *
 @Override public
 void Call (SUBSCRIBER< Super bitmap> subscriber) {
  //Get the Bitmap object
  of the picture through the URL Bitmap Bitmap = getbitmapforurl.getbitmap (URL);
  Callback Observer Method
  Subscriber.onnext (bitmap);
  Subscriber.oncompleted ();
  LOG.I ("Call--->", "Run on" + Thread.CurrentThread (). GetName () + "thread");
 }
)

And then we need to create observer and subscribe, and here's the chained call

. Subscribe (New observer<bitmap> () {//Subscribe observer (in fact, observer subscribing to observer) @Override public
 void OnNext (Bitmap Bitmap) {
  Mainimageview.setimagebitmap (bitmap);
  LOG.I ("OnNext--->", "Run on" + Thread.CurrentThread (). GetName () + "thread");

 @Override public
 void oncompleted () {
  mainprogressbar.setvisibility (view.gone);
  LOG.I ("OnCompleted--->", "Finish");
 }

 @Override public
 void OnError (Throwable e) {
  log.e ("OnError--->", e.tostring ();
 }
 );

Of course, network requests are time-consuming operations, we need to execute them in other threads, and updating the UI needs to be done in the main thread, so we need to set up the thread:

. Subscribeon (Schedulers.io ())//Specifies that subscribe () occurs in IO threads
. Observeon (Androidschedulers.mainthread ())// Specifies that the callback for subscriber occurs in the UI thread

So we have completed a Rxjava basic writing, now look at the whole code:

Create the Observer Observable.create (new Observable.onsubscribe<bitmap> () {/** * Replication Call method * * @param subscriber Observer Object * /@Override public void Call (SUBSCRIBER&LT. Super bitmap> Subscriber) {//The Bitmap object of the picture through the URL Bitmap Bitmap = Get
  Bitmapforurl.getbitmap (URL);
  Callback Observer Method Subscriber.onnext (bitmap);
  Subscriber.oncompleted ();
 LOG.I ("Call--->", "Run on" + Thread.CurrentThread (). GetName () + "thread"); }). Subscribeon (Schedulers.io ())//Specifies that subscribe () occurs in IO threads. Observeon (Androidschedulers.mainthread ())// The callback for the specified subscriber occurs in the UI thread. Subscribe (new observer<bitmap> () {//Subscribe observer (in fact, observer subscribing to observer) @Override public void OnNext (Bi
  TMap bitmap) {mainimageview.setimagebitmap (bitmap);
 LOG.I ("OnNext--->", "Run on" + Thread.CurrentThread (). GetName () + "thread");
  @Override public void oncompleted () {mainprogressbar.setvisibility (view.gone);
 LOG.I ("OnCompleted--->", "complete"); @Override public void OnError (Throwable e) {log.e ("OnError--->", e.tostring ()); }
 });

Okay, here's the dynamic diagram that runs:


Rxjava Load Network Pictures asynchronously

Now look at the log logs that are running:


Log

As you can see, the call method (event generation) executes on the IO thread, while the OnNext method (event consumption) executes on the main thread. Description of the previous analysis is right.

Summarize

Well, because this is a Rxjava foundation, it's a little too long. Even so, many of the details of the problem are not clearly explained. But fortunately, this article has already rxjava the necessary basic introductory knowledge. Perhaps due to the technical level is limited, the article will inevitably have mistakes or negligence, welcome to correct and exchange. Hope this article for everyone's study or work to bring some help, small series will update the relevant articles, interested friends please continue to pay attention to the cloud habitat community.

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.