The Scheduling and Threading section of Intro to Rx says that
the use of SubscribeOn and ObserveOn should only be invoked by the final subscriber
It also says that in an UI application, the presentation layer, which is normally the final subscriber, should be the one to invoke these methods.
I am wondering if the advice is solid, since I see some situations in which this is not convenient:
subscribeOn()
before returning the Observable, passing the IO Scheduler or the immediate Scheduler as convenient.subscribeOn()
the UI Scheduler, then observeOn()
some other Scheduler, and finally observeOn()
the UI Scheduler. In this case, being able to invoke subscribeOn()
and observeOn()
only in the final subscriber would mean that the stream can only be processed in the UI thread.Is there some good reason why I should sacrifice the architecture of my application and ignore Rx's ability to easily switch threads by invoking these two methods only by the final subscriber?
Great to see you have read the book and are taking the time to challenge some of the guidance there.
The reason I give this guidance is because
These are obviously my opinions but I have seen these simple guidelines help clean up code on dozens of projects, reduce code bases, improve test ability, improve predictability and in numerous case massively improve performance.
Sadly, it is difficult to put together case studies of these projects as most of them are protected by NDAs.
I would be keen to see how it works for you or how you apply an alternate pattern.