RxJava 2.x Understanding-1

Source: Internet
Author: User
Tags getmessage throwable

In the Rxjava 1.x series, explained the Rxjava's approximate usage, because now all uses Rxjava 2, therefore Rxjava 1 is not fine-spoken, mainly to study Rxjava 2.

Basic use:
    /*** Basic use of RAJAVA2*/    Private voidRxjava2baseuser () {Observable. Create (NewObservableonsubscribe<string>() {@Override Public voidSubscribe (@NonNull observableemitter<string> emitter)throwsException {emitter.onnext ("1"); Emitter.onnext ("2"); Emitter.onnext ("3"); //throw new Exception ("error occurred");}}). Subscribe (NewObserver<string>() {disposable disposable; //Add this method@Override Public voidOnsubscribe (@NonNull disposable D) {log.d (TAG,"Onsubscribe"); Disposable=D; } @Override Public voidOnNext (@NonNull String s) {log.d (TAG,"Item:" +s); if(S.equals ("4") ) Disposable.dispose (); //in Rxjava 2.x, the new disposable can be cut off, allowing observer observers to no longer receive upstream events} @Override Public voidOnError (@NonNull throwable e) {log.d (TAG,"OnError:" +e.getmessage ()); } @Override Public voidOnComplete () {log.d (TAG,"OnComplete");        }                }); Observable. Create (NewObservableonsubscribe<string>() {@Override Public voidSubscribe (@NonNull observableemitter<string> emitter)throwsException {emitter.onnext ("----------"); Emitter.onnext ("----------"); Emitter.onnext ("----------"); }                })                //Consumer and Action1 in RxJava 1 are similar. Subscribe (NewConsumer<string>() {@Override Public voidAccept (String s)throwsException {log.d (TAG,"Item:" +s);    }                }); }

There's no difference between basic use and Rxjava 1.

1. The Onsubscribe method is added, and the Onsubscribe method is triggered at the beginning of the event.

2. The new disposable can be cut off, allowing observer observers to no longer receive upstream events.

3.action1--Consumer only accepts OnNext methods.

4. If there are multiple DisposableWhat to do, a container has been built into the Rxjava CompositeDisposable, whenever we get a DisposableWhen you call CompositeDisposable.add()Add it to the container, and at the exit, call CompositeDisposable.clear()You can cut off all the pipes. Thread Switching:
/*** RAJAVA2 Thread*/    Private voidRxjava2thread () {Observable. Create (NewObservableonsubscribe<string>() {@Override Public voidSubscribe (@NonNull observableemitter<string> emitter)throwsException {log.d (TAG,"Event Processing thread:" +Thread.CurrentThread (). GetName ()); Emitter.onnext ("----1----"); Emitter.onnext ("----2----"); Emitter.onnext ("----3----"); }}). Subscribeon (Schedulers.newthread () )//indicates the thread being processed by the observer. Observeon (Androidschedulers.mainthread ())//indicates the Observer thread. Subscribe (NewObserver<string>() {@Override Public voidOnsubscribe (@NonNull disposable D) {log.d (TAG,"Onsubscribe:" +Thread.CurrentThread (). GetName ()); } @Override Public voidOnNext (@NonNull String s) {log.d (TAG,"Item:" + S + ":" +Thread.CurrentThread (). GetName ()); } @Override Public voidOnError (@NonNull throwable e) {log.d (TAG,"OnError:" + e.getmessage () + ":" +Thread.CurrentThread (). GetName ()); } @Override Public voidOnComplete () {log.d (TAG,"OnComplete:" +Thread.CurrentThread (). GetName ());    }                }); }

Results:

02-10 10:02:31.007 25414-25414/pers.bolin.rxjava2demo d/MainActivity:onSubscribe:main02-10 10:02:31.009 25414-25970/pers.bolin.rxjava2demo d/mainactivity: Event processing thread: rxnewthreadscheduler-102-10 10:02:31.047 25414-25414/ Pers.bolin.rxjava2demo D/mainactivity:item:----1---- : main02-10 10:02:31.048 25414-25414/ Pers.bolin.rxjava2demo D/mainactivity:item:----2---- : main02-10 10:02:31.048 25414-25414/ Pers.bolin.rxjava2demo D/mainactivity:item:----3----: Main

You can see that the event has been divided into different threads to handle.

                . Subscribeon (Schedulers.newthread ())        //indicates the thread being processed by the Observer                . Observeon (Androidschedulers.mainthread ())  // Indicates the Observer thread

It is important to note that the Subscribeon is only valid for the first switch, observeon each switch is valid

See what the parameters of the thread are:

    • Schedulers.io () A thread representing IO operations, typically used for IO-intensive operations such as network, read-write files, etc.
    • Schedulers.computation () represents CPU-intensive operations, such as operations that require a lot of computation
    • Schedulers.newthread () represents a regular new thread
    • Androidschedulers.mainthread () represents the main thread of Android
    • Schedulers.single () represents a default, shared, single-threaded, supported scheduler instance that performs strong sequential execution on the same background thread.
    • Schedulers.trampoline () means that when the other queued tasks are completed, the current thread is queued to begin execution
Transform/Operator:

Resources:
Https://www.jianshu.com/p/8818b98c44e2

RxJava 2.x Understanding-1

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.