I am just learning Rx-java and Rxandroid2 and I am just confused what is the major difference between in SubscribeOn and ObserveOn.
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.
tl;dr If none of the above makes any sense, look at this code snippet
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 on the UI thread, now switch to a Computation Thread and perform
map(length -> 2 * length)function now make sure you Observe the output on Main thread but perform all the tasks defined undersubscribe()in a IO thread.
Source : Tomek Polański (Medium)