[Android Development] RXJAVA2 Road Two-basic methods of use __java

Source: Internet
Author: User
first, what is Rxjava 1.1 Introduction

A library that uses observable sequences on a Java JVM to compose asynchronous, event-based programs.
The concept is very complex, not so-called. Let's learn to use it first.

Rxjava GitHub Address:
Https://github.com/ReactiveX/RxJava
Rxjava GitHub Address:
Https://github.com/ReactiveX/RxAndroid

It is recommended to view his wiki, to see its contents and to understand it throughout. 1.2 Three Concepts Observable: Observed by (subject subject) Observer/subscriber: Observer Subscribe: Subscribe

Observable and observer implement subscription relationships through the Subscribe () method

It was previously done by add to the viewer to subscribe and then change to notify. Rxjava is sent once you subscribe. Ii. The use of the trilogy

The premise is to rely on the first

Compile ' io.reactivex.rxjava2:rxandroid:2.0.1 '
//Because Rxandroid releases are few and far, it is between D also
//explicitly depend on Rxjava ' s latest version for bug fixes and new features.
Compile ' io.reactivex.rxjava2:rxjava:2.0.1 '
2.1 Method 1:create

2.1.1 Created by the Observer observable

/**
 * Defines the observed
 * @return
 /public
observable<string> getobservable () {
    return Observable.create (New observableonsubscribe<string> () {
        @Override public
        void Subscribe ( Observableemitter e) throws Exception {
            //Only three methods OnNext, OnError, oncompleted

            //onnext similar to
            Observer mode change E.onnext ("HI");  Send data
            e.onnext ("balance");  Send data

            //launch completes, this method needs to call oncompleted manually, will callback observer oncompleted method
            E.oncomplete ();

            OnComplete and OnError only call one, two are used only to recognize the front of the
            //e.onerror (new Exception ("Wrong!");
        }
    );

2.1.2 Create observer Observer

/** * Generate Observer * @return/Public observer<string> Getobserver () {return new observer<string> () {

            @Override public void OnError (Throwable e) {log.d (TAG, "OnError:" +e.getmessage ());
            An error call occurred @Override public void OnComplete () {LOG.D (TAG, "oncompleted:"); Call}/** * * @param d/@Override public void o When data reception is complete Nsubscribe (disposable D) {//First callback, no execution OnNext, OnComplete, OnError will also callback Log.d (TAG, "Onsubscribe:" +d.t

            Ostring ());  D.dispose (); Remove subscription relationship//d.isdisposed ();
        The decision to cancel the subscription relationship, for the truth is not a subscription, false is a subscription in the/** * The Observer calls OnNext here will be callback * @param s parameter * *

            @Override public void OnNext (String s) {log.d (TAG, OnNext: +s);  Normal receive data call System.out.print (s); will receive greetings from sender "Hi,weavey."

"
        }
    };
 }

2.1.3 Subscriptions

@Override
protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.activity_main);



    Observable<string> observable = getobservable ();
    observer<string> Observer = Getobserver ();

    The associated observer and the observed-> subscribe to
    OBSERVABLE.SUBSCRIBE (Observer);
}
2.2 Method 2:create

2.2.1 To create the observed person
2.2.2 Create a viewer

Already contains subscriptions

Observable.subscribe (New consumer<string> () {
            @Override public
            Void Accept (String s) throws Exception { c2/>//This accept equals the OnNext LOG.E of the Observer
                (TAG, "Accept:" +s);
            }
        , New consumer<throwable> () {
            @ Override public
            Void Accept (Throwable throwable) throws Exception {
                //onerror
            }
        }, new Action () {
            @Override public
            Void Run () throws Exception {

            }
        });
2.3 Method 3:just

2.3.1 Generation of observed persons

/**
 * Generate
 the observed * @return/public
observable<string> getobservable () {
    //send "Just1" in turn and "Just2" Return
    observable.just ("Just1", "Just2");
}

2.3.2 defines the Observer

Contains subscriptions

Observable<string> observable = getobservable ();

Observable.subscribe (New consumer<string> () {
    @Override public
    Void Accept (String s) throws Exception { c3/>//This accept equals the OnNext LOG.E of the Observer
        (TAG, "Accept:" +s);
    }
);
2.4 Method 4:fromarray

2.4.1 Generation of observed persons

/**
 * Generate the observed
 * @return
 /public
observable<string> getobservable () {
    return Observable.fromarray ("From1", "From2", "from3");
}
Defines the observer as consistent with the above 2.5 method 5:fromcallable

2.5.1 generation of observed persons

/**
 * Generate the observed
 * @return
 /public
observable<string> getobservable () {
    return Observable.fromcallable (New callable<string> () {
            @Override public
            String call () throws Exception {
                return "fromcallable";
            }
        );

2.5.2 defines the observer as consistent with the above

There are others to look at the list below. three. Method List

td> converts a iterable, a future, or an array to a observable The The
name parsing
Just () converts one or more objects to a observable that launches this or one of these objects.
fromarray ()
repeat () to create a duplicate launch of the specified data or sequence of data observab Le
repeatwhen () creates a observable that repeats the specified data or sequence of data, depending on the data fired by another observable
Create () creates a observable from scratch using a function
defer () creates observable only when subscribers subscribe, and creates a new observable for each subscription
Range () creates a observable that emits an integer sequence of the specified range
interval () creates a observable that emits an integer sequence at a given interval of time.
timer () creates a observable that emits a single data after a given delay
Empty () creates a observable with no direct notification complete
error () creates a observable that does nothing to make a direct notification error
never () creates a observable that does not emit any data

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.