Rxjava operator (i) creating Observables__java

Source: Internet
Author: User
Rxjava is just a Java implementation of Reactivex (reactive Extensions), Reactivex is a responsive extension framework with many implementations, such as Rxandroid, RXJS, Rxswift, Rxruby, and so on. Rx uses a form similar to the observer to achieve a variety of functions, with our general writing code thinking of a greater difference. Initial contact may be difficult to understand, but once you have mastered it, you will feel the strength of it. The principle is to create a observable object to work, and then use a variety of operators to build the chain operation, as the assembly line of the data you want to process step-by-Step into the finished product you want to launch (emit) to subscriber.

Rxandroid is an extension of Rxjava on Android, and if you're an Android developer, the various main threads and threading operations will certainly make you feel a headache, and rxandroid can easily solve your problems. In order to facilitate testing and compiling, this paper's demo program is based on Rxadroid to achieve.

Rx's power is in its rich operator, so you have to be flexible to use the RX, you have to master these operators, let's first look at how to create observable operators.

A, Create
Create is the most basic creation of the observable operator, its schematic diagram is as follows (the schematic diagram in this article uses the official website picture)

The most important thing to create a observable is to call the Subscriber Onnext/oncomplete/onerror method at the right time. OnNext is the launch of the processing of data to subscriber; OnComplete used to tell Subscriber that all the data had been launched, and that OnError would have fired a Throwable object to subscriber in the event of an error. One thing to note is that observable must call all the Subscriber OnComplete methods and only call once, and the OnError method is the same when the error occurs, and no other method of subscriber can be invoked once the call is made. The following is the use of the Create operator:

Click (here) to collapse or open private observable<integer> createobserver () {
Return Observable.create (New observable.onsubscribe<integer> () {
@Override
public void Call (SUBSCRIBER&LT. Super integer> Subscriber) {
if (!subscriber.isunsubscribed ()) {
for (int i = 0; i < 5; i++) {
int temp = new Random (). Nextint (10);
if (Temp > 8) {
If value>8, we make an error
Subscriber.onerror (New Throwable ("Value >8"));
Break
} else {
Subscriber.onnext (temp);
}
On Error,complete the job
if (i = = 4) {
Subscriber.oncompleted ();
}
}
}
}
});
In this method, we create and return a observable, which produces 5 random numbers less than 10 and emits them sequentially, if the random number is greater than 8, we think it is an error. Here is our use of this observable:
Click ( here ) collapse or openMlbutton.setonclicklistener (e-> createobserver (). Subscribe (New subscriber<integer> () {
@Override
public void oncompleted () {
Log ("oncomplete!");
}

@Override
public void OnError (Throwable e) {
Log ("OnError:" + e.getmessage ());
}

@Override
public void OnNext (integer integer) {
Log ("OnNext:" + integer);
}
})); When the button is clicked, we create a Subscriber object and register it with the created observable object and receive the data it launches. The test was a total of two clicks, the first successful launch of 5 data, the second time after the launch of 2 data generated errors. The results of the operation are as follows:


Two, Range
The range operator launches a series of m values greater than or equal to N, based on the initial value N and number m of the entry and exit.

It is also very convenient to use, only to set the initial value and number on it, do not have to implement the call to Subscriber

Click (here) to collapse or open private observable<integer> rangeobserver () {
Return Observable.range (10, 5);
Subscribe to it:

Click (here) to collapse or open Mrbutton.setonclicklistener (e-> rangeobserver (). Subscribe (i-> log (i)));
Run results output 10-14 of 5 numbers:


Third, Defer, Just
The defer operator creates a new observable object only when there is a Subscriber subscription, which means that every subscription gets a newly created observable object, which ensures that the data in the observable object is up to date. Its characteristics we will be below and just to compare and understand.


The just operator converts an object into a observable object and launches it so that a number, a string, an array, a iterate object, and so on, is a very quick way to create a observable object, and will be used in a number of cases in the future.

Let's use defer and just to create a observable to return the current number of milliseconds, respectively.

Click (here) to collapse or open private observable

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.