0
votes

Rxjava can change the thread schedule of observable and subscriber with the observeOn method and the subscribeOn method. The observeOn and subscribeOn use the lift method to return a new Observable.
The lift method will create a new observable, I think it like a clone method,it will create a new subscriber and a new OnSubscribe.

In observeOn():the new OnSubscribe will notify the old OnSubscribe to send messages to the new subscriber and the new subscriber will change its thread scheduler when it gets the message.After this, the new subscriber will send messages to the old subscriber.

In subscribeOn():The new OnSubscribe will change its thread scheduler first and then it'll notify the old OnSubscribe.........and so on.

My question is why the subscribeOn() change its thread scheduler in the new OnSubscribe first and the observeOn() change its thread scheduler when the new subscriber get messages from the old onSubscriber? Why the creator designed it like this? Why the place of changing thread scheduler in subscribeOn and observeOn is different?
Thanks in advance

1

1 Answers

0
votes

The subscribeOn allows handling the subscription side-effects by moving it to another thread (lifecycle: subscription-time). The observeOn allows handling the observation side-effect of each value emitted by moving the emission to another thread (lifecycle: runtime). These two act at different points in the lifecycle but sometimes the net outcome is the same. The call directions are opposite to each other: subscribeOn goes upstream and observeOn goes downstream.