What is the difference between the subscribeOn and observeOn methods?

Do I understand him correctly?

Here is how I understand it: subscribeOn determines the default stream for Observable after it is created (if it needs to be executed not in the current stream), so the execution will always start in the stream defined by subscribeOn . And therefore, only one subscribeOn needed (if there is several subscribeOn , only the first one will be executed). And observeOn after can change the flow, starting from the place of the call, and how many will observeOn , so many times the flow will change.

  • 2
    Here is the answer to your question. And, in general, you understand correctly. - mit

1 answer 1

I found a useful article that contains the exact answer about the number and place of calling these methods. Hope to help someone else.

About subscribeOn ()

The subscribeOn () operator will be subject to the same observable chain; however, you can't use multiple subscribeOn () operators in the same chain. If you want to subscribeOn (), then you need to subscribeOn ()

What means:

The subscribeOn () statement will have the same effect no matter where you put it in the observable chain; however, you cannot use multiple subscribeOn () statements in the same chain. If you chained more than one subscribeOn (), your chain will only use subscribeOn (), which is closest to the observable source.

subscribeOn() , which is closest to the Observable source, is the first in the chain, and this statement was also verified empirically, for example, if there is a chain:

 observable .subscribeOn(Schedulers.newThread()) .subscribeOn(Schedulers.computation()) .subscribe(observer); 

then the onNext() each element is executed in the RxNewThreadScheduler-1 stream (the stream created by Schedulers.newThread() ), and the chain

 observable .subscribeOn(Schedulers.computation()) .subscribeOn(Schedulers.newThread()) .subscribe(observer); 

passes the onNext() execution of each element to the RxComputationThreadPool-1 stream (the stream created by Schedulers.computation() ).

About observeOn ()

Unlike subscribeOn (), where it’s downstream. For example, it’s possible that you’ll use it.

 .observeOn(Schedulers.newThread()) 

This is the case where the operator will observe it. Where you can send their notifications by inserting multiple observeOn () operators your chain.

What means:

Unlike subscribeOn (), it matters where in the chain you place the observeOn () function, since this operator only changes the stream that is used by the observables, which follow below. For example, if you insert the following code into your chain, then each observable that appears in the chain from now on will use the new stream.

 .observeOn(Schedulers.newThread()) 

This chain will continue to work in the new stream until another observOn () operator is encountered, after which it will switch to the stream indicated by this operator. You can control the flow where specific observables send their notifications by inserting several observeOn () operators into your chain.