In Rx, it is usually recommended to avoid side effects in the "do" blocks (which will only be executed if the stream is signed) and prefer to subscribe to the code.
In your case, you can use cache() or publish()...connect() , for example:
query = apiService.getObjects(token) .compose(bindToLifecycle()) .subscribeOn(Schedulers.io()) .cache(); query.observeOn(AndroidSchedulers.mainThread()) .subscribe(o -> { // process in Main Thread }) query.observeOn(Schedulers.io()) .subscribe(o -> { // process in the background thread });
With publish() instead of cache() code is identical, but you can decide when to run the request by connecting the stream (you call query.connect() after connecting 2 subscriptions).
If your subscription work is background computing, Schedulers.computation() might be preferable to Schedulers.io() .
Please note that AFAICT your code will work fine, without the map(Observable::just) line map(Observable::just) , since the "observOn" statements only affect the down stream (and not the previous "do")
Gluck source share