Understanding subscribeOn()
We kind of touched on using subscribeOn()
already, but in this section, we will explore it in more detail and look at how it works.
The subscribeOn()
operator will suggest to the source Observable
upstream which Scheduler
to use and how to execute operations on one of its threads. If that source is not already tied to a particular Scheduler
, it will use the Scheduler
you specify. It will then push emissions all the way to the final Observer
using that thread (unless you add observeOn()
calls, which we will cover later). You can put subscribeOn()
anywhere in the Observable
chain, and it will suggest to the upstream all the way to the origin Observable
which thread to execute emissions with.
In the following example, it makes no difference whether you put this subscribeOn()
right after Observable.just()
or after one of the operators. The subscribeOn()
will communicate upstream to the Observable.just()
which Scheduler
to use no matter where you put it. For clarity...