RxJava Schedulers-observeOn() & subscribeOn()
RxJava does not include concurrency on its own. Scheduler creates an instance of a worker that is responsible for scheduling and running code. There are 2 main operators that allow users to use scheduler ie. subscribeOn() and observeOn().
We have some builtin schedulers like Scheduler.io(), Scheduler.computation(), Scheduler.newThread() and many more, you can read them from here. In this article mainly we will try to understand observeOn and subscribeOn, also the difference between the two.
Let's start with basic code, without any observeOn or subscribeOn
So we see that everything runs on the same thread. By default, everything in RxJava runs on the same thread. Now let's see what happens when we use various combination of subscribeOn and observeOn.
subscribeOn()
By inserting subscribeOn anywhere between observable and subscribe, we select scheduler where OnSubscribe callback will be called. Let's check with an example. This is important because we don't want to run long-running operations on the main thread or we want to distribute work on different threads.
Now we can see call operation are moved to computation threads. Now let's see what happen if we have multiple subscribeOn. Which will get honoured.
As we can see, subscribeOn() that came first in the chain is honoured. If we switch the computation and newThread scheduler we will get RxNewThreadScheduler-1 instead of RxComputationThreadPool-1 everywhere in the output. Let's check observeOn and then we will try to use both together.
observeOn()
observeOn controls which schedulers is used in downstream subscribers. Any code inside create is controlled by subscribeOn, now we might need to do a task in a background thread and update the UI thread after this. This is the most common use case in android development, observeOn comes to the rescue. Let's understand more by examples.
We can see, that after observeOn everything runs on RxNewThreadScheduler. This is what we meant by downstream above. Let's see what happens if we have multiple observeOn.
Above example clearly shows that unlike subscribeOn, every time we use observeOn downstream scheduler changes. But we still see that Subscribed is still in the main thread. From all the above examples, we see that what can be controlled by subscribeOn and what by observeOn. Let's use them together to make things more clear.
subscribeOn and observeOn
We can notice 2 main things
1. As soon as we added subscribeOn(), Subscribed is now not in the main thread instead it uses scheduler provider by subscribeOn.
2. Even though we added subscribeOn after the first map, the second map still uses scheduler provided by the first onbserveOn().
But if we remove all observeOn(), everything will be controlled by Scheduler.io(). Also adding more subscribeOn after the first one won't change the output.
From the above examples, we can clearly see that
- Every time we call subscribe on an observable, onSubscribe callback is invoked. This is passed in create, and by default, everything happens on the same thread.
- By adding subscribeOn we actually provide scheduler on with this onSubscribe callback should be invoked. In the examples above, we see how thread name in Subscribed Log changes with subscribeOn and also how multiple subscribeOn behaves.
- observeOn helps us to control the scheduler for downstream and has no impact on the ObservableOnSubscribe and can be used to switch schedulers between operators.
For getting updates for interesting articles related to tech and programming to join TechShots.Its a start to a long journey. We will love the developers to be a part of this and publish blogs related to any tech they like. You can also send us suggestions at techshotscommunity@gmail.com.Your feedback is very valuable.
You can also follow us on Facebook for all the updates.