After digging into RxJava threading last two days found the rule of thumbs for handling RxJava Threading/Scheduling:
observeOn
works only downstream operator
subscribeOn
works for both downstream and upstream operator
- Consecutive/Multiple
subscribeOn
do not change the thread
- Consequent
observeOn
do change the thread for downstream oerator
- Unlike with
subscribeOn()
, we can use observeOn()
multiple times for
seamless thread switching
- Operator like
delay()
, interval()
have default scheduler and can change the downstream scheduler as well
So, for my case:
Completable.complete() // IO scheduler based on subscribeOn scheduler
.subscribeOn(http://Schedulers.io ())
.observeOn(AndroidSchedulers.mainThread())
.delay(5000, TimeUnit.MILLISECONDS) // Default Computation scheduler
.doOnComplete(() -> liveDataState.postValue("")) // Computation scheduler by delay scheduler
.subscribe() // Computation scheduler by delay as well
Also, you can look into the marble diagram for more understanding:
