After reading multiple blog posts and documentation, I came to the conclusion that following doOnSubscribe
will be executed on a worker thread:
Observable.just(1)
.observeOn(Schedulers.io())
.doOnSubscribe(__ -> Log.d("Testing", "Testing")) // Shouldn't this be on worker thread?
.subscribe();
But after debugging, I see doOnSubscribe
is executed on main thread. I thought doOnSubscribe
is similar to other operators and hence has similar threading behavior when coupled with subscribeOn
and observeOn
.
What am I missing? How can I move doOnSubscribe
execution to background thread?
subscribeOn
andobserveOn
have no effect ondoOnSubscribe
because the connection between operators are established on the caller thread in order to support immediate cancellation. You have to defer the subscription to adoOnSubscribe
in some way, e.g.:or