Rxandroid What's the difference between SubscribeOn and ObserveOn
I am just learning Rx-java and Rxandroid2 and I am just confused what is the major difference between in SubscribeOn and ObserveOn.
SubscribeOn specify the Scheduler on which an Observable will operate. ObserveOn specify the Scheduler on which an observer will observe this Observable.
So basically SubscribeOn is mostly subscribed (executed) on a background thread ( you do not want to block the UI thread while waiting for the observable) and also in ObserveOn you want to observe the result on a main thread...
If you are familiar with AsyncTask then SubscribeOn is similar to doInBackground method and ObserveOn to onPostExecute...
In case you find the above answer full of jargons:
tl;dr
Observable.just("Some string")
.map(str -> str.length())
.observeOn(Schedulers.computation())
.map(length -> 2 * length)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(---)
Observe an observable... perform the map function in an IO thread (since we are
"subscribingOn"
that thread)... now switch to a Computation Thread and performmap(length -> 2 * length)
function... and finally make sure you Observe the output on (observeOn()
) Main thread.
Anyway,
observeOn()
simply changes the thread of all operators further Downstream. People usually have this misconception that observeOn
also acts as upstream, but it doesn't.
The below example will explain it better...
Observable.just("Some string") // UI
.map(str -> str.length()) // UI
.observeOn(Schedulers.computation()) // Changing the thread
.map(length -> 2 * length) // Computation
.subscribe(---)
subscribeOn()
only influences the thread which is going to be used when Observable is going to get subscribed to and it will stay on it downstream.
Observable.just("Some String") // Computation
.map(str -> str.length()) // Computation
.map(length -> 2 * length) // Computation
.subscribeOn(Schedulers.computation()) // -- changing the thread
.subscribe(number -> Log.d("", "Number " + number));// Computation
Position does not matter (
subscribeOn()
)
Why? Because it affects only the time of subscription.
Methods that obey the contact with
subscribeOn
-> Basic example : Observable.create
All the work specified inside the create
body will run on the thread specified in subscribeOn
.
Another example: Observable.just
,Observable.from
or Observable.range
Note: All those methods accept values, so do not use blocking methods to create those values, as subscribeOn won't affect it.
If you want to use blocking functions, use
Observable.defer(() -> Obervable.just(blockingMenthod())));
Important Fact:
subscribeOn does not work with Subjects
Multiple
subscribeOn
:
If there are multiple instances of subscribeOn
in the stream, only the first one has a practical effect.
Subscribe &
subscribeOn
People think that subscribeOn
has something to do with Observable.subscribe
, but it doesn't have anything special to do with it.
It only affects the subscription phase.
Source : Tomek Polański (Medium)