Skip to content Skip to sidebar Skip to footer

How Rxjava Scheduler/threading Works For Different Operator?

Can anyone please help me to explain which scheduler are running below code? Completable.complete() .subscribeOn(http://Schedulers.io ()) .observ

Solution 1:

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:

enter image description here

Post a Comment for "How Rxjava Scheduler/threading Works For Different Operator?"