Rxjava Series One of the first knowledge Rxjava

Source: Internet
Author: User

1. Introduction

Basic knowledge

The basic component of the responsive code is observables and subscribers (in fact Observer is the smallest building block, but the most used in practice is subscriber, because Subscriber is the counterpart of Observables. )。
Observable sends messages, while Subscriber is used to consume messages. The message is sent in a fixed mode. Observable can send any number of messages (including empty messages),
When a message is successfully processed or an error occurs, the process ends. Observable will call each of its subscriber Subscriber.onnext () functions and End with Subscriber.oncomplete () or Subscriber.onerror ().
This looks like the standard observer pattern, but a different key point is: observables generally only wait until there is a subscriber subscription to it,
To start sending messages (the term is hot-start observable and cold-start observable. Hot start observable will send messages at any time,
Even if no observer listens to it. Cold start observable messages are sent only when there is at least one subscriber (in my case, there is only one subscriber). This difference is not important to start learning Rxjava. )。
In other words, if no subscriber observes it, it will not work.

2. First Rxjava procedure

Well, you want to use Rxjava in your project, just by adding the following in Gradle:
Compile ' io.reactivex:rxjava:1.0.13 ' current version (I use Gradle, you can view other deployments of Rxjava via GitHub)

First, write a small example:

(here only a portion of the code is pasted in all the code, see the bottom of the article) through the Rxjava to TextView set content. (This is just a demo, and there's no egg) first create observablePrivateObservable<string>Cerateobserver() {returnObservable.create (NewObservable.onsubscribe<string> () {@Override             Public void Pager(SUBSCRIBER&LT;?Superstring> subscriber) {Subscriber.onnext ("First Rxjava");            Subscriber.oncompleted ();    }        }); } and create a subscriberPrivateSubscriber<string>Createsubscriber() {return NewSubscriber<string> () {@Override             Public void oncompleted() {log.i (TAG,"on completed"); }@Override             Public void OnError(Throwable e) {LOG.I (TAG,"On Error"); }@Override             Public void OnNext(String s) {LOG.I (TAG,"Next Value ="+ s);            Mtextview.settext (s);    }        }; }//finally tie him up. Observable.subscribe (subscriber);

OK, it's out.

In order to print that few words write so much code is also drunk.
Well, let's look at the more concise code below.

//下面的代码乍一看可能难以接受, 慢慢来        Observable.just("第二个RxjavaDemo").subscribe(new Action1<String>() {            @Override            publicvoidcall(String s) {                    mTextView.setText(s);            }        });

From the code above, we've used two new things.
1. Observer's just and Action1

In fact, Observer provides us with a lot of quick and easy operation. And just's role is to send only one task and then end. And action we can understand as one of the subscriber.
For example: There are three functions in subscriber oncompleted OnError OnNext We can interpret them as three action
And Action1 represents only one parameter, similar reasoning: Will there be Action2, Action3 and so on, waiting for everyone to explore
Of course, if you use a JDK that is 1.8, you can optimize the code by Lambdas expressions:

Observable.just("第二个RxJavaDemo lambdas").subscribe(t -> mTextView.setText(t));

Is not the moment to feel the bunker.

Well, we've learned how to use just.

In fact, observable provides a lot of operators to simplify the writing of our code.
The function of the operator is to modify the events emitted by the observable between the observable and the final subscriber.

Here's what we'll cover: the simple usage of the map operator the map operator is the function of converting one event to another event.
For example, for example, the server now returns a string, and I need to re-stitch the string into a new string and load the data.
At this point, we can operate through the map.

observable.just (URL). Map (new  func1< String, string> () { @Override  pub Lic  String call  (String s) {return   "http://www.baidu.com/"  + S; }}). Subscribe (new  action1<string> () { @Override  public  void  call  (String s) {mtextview.settext (s); } }); //if you have LAMDBA  Observable.just (URL). map (F- "http://www.baidu.com/"  + F). Subscribe (T- Mtextview.settext (t)); 

The map operator is characterized by: you pass a observable object to him, he returns you a observable object, but does not need to correspond to the observable return type. In the next section we will describe the operation of the map in detail, and will take the Rxjava version of Rxandroid to write a simple network request. Please look forward to ...
(The article refers to the official, as well as the works of several other great gods)

Source Address: Click to enter

Welcome to my public number: Androidesigner
By scanning the QR code can be concerned.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Rxjava Series One of the first knowledge Rxjava

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.