RxJava Source Analysis (2)

Posted by Danestar on Tue, 30 Jun 2020 18:16:07 +0200

This time we are talking about the thread switching process of RxJava. First, look at a diagram:

The RxJava code for this time series diagram:

    public void demo2(){
        createObservable()
                .subscribeOn(Schedulers.newThread())//Observed executes subscribe on a new thread
                .observeOn(AndroidSchedulers.mainThread())//The observer executes onNext onComplete on the main thread
                .subscribe(createObserver());
    }
    //Create an observer
    private Observer createObserver() {
        return new Observer() {
            @Override
            public void onSubscribe(Disposable d) {
                printThreadId("onSubscribe");
            }

            @Override
            public void onNext(Object o) {
                printThreadId("onNext" + o);
            }

            @Override
            public void onError(Throwable e) {
                printThreadId("onError" + e.getMessage());
            }

            @Override
            public void onComplete() {
                printThreadId("onComplete");
            }
        };
    }

    //Create Observed
    private Observable createObservable() {
        return Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter e) throws Exception {
                printThreadId("subscribe");
                e.onNext(1);
                e.onComplete();
            }
        });
    }

Execution results:

    /**
     12-06 15:34:35.577 27544-27544/com.study.rxjavademo I/ThreadDemos: onSubscribe thread_name: main
     12-06 15:34:35.587 27544-27608/com.study.rxjavademo I/ThreadDemos: subscribe thread_name: RxNewThreadScheduler-1
     12-06 15:34:35.627 27544-27544/com.study.rxjavademo I/ThreadDemos: onNext1 thread_name: main
     12-06 15:34:35.627 27544-27544/com.study.rxjavademo I/ThreadDemos: onComplete thread_name: main
     */

What does the code above mean?
We can simulate a network request with the above code:
1. The observee is a network request

    //Create Observed
    private Observable createObservable() {
        return Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter e) throws Exception {
                printThreadId("subscribe");
                e.onNext(1);
                e.onComplete();
            }
        });
    }

The subscribe method performs a time-consuming operation, so we should let it execute on a non-primary thread.

2. The observer is equivalent to a callback waiting for the network to return

    //Create an observer
    private Observer createObserver() {
        return new Observer() {
            @Override
            public void onSubscribe(Disposable d) {
                printThreadId("onSubscribe");
            }

            @Override
            public void onNext(Object o) {
                printThreadId("onNext" + o);
            }

            @Override
            public void onError(Throwable e) {
                printThreadId("onError" + e.getMessage());
            }

            @Override
            public void onComplete() {
                printThreadId("onComplete");
            }
        };
    }

We want some ui adjustments on onNext returns, so we should set it on the main thread.
3. How do I set it on which thread?

                .subscribeOn(Schedulers.newThread())//Observed executes subscribe on a new thread
                .observeOn(AndroidSchedulers.mainThread())//The observer executes onNext onComplete on the main thread

Now there are scenes and results.So how do these work in RxJava?
Next, the source code for the entire process is analyzed.

Topics: network