O'Reilly logo

Reactive Android Programming by Tadas Subonis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

observeOn

.observeOn() works a bit differently than .subscribeOn(). Whenever .observeOn() is called, it changes the thread of downstream where the code is getting executed.

Consider that we have a flow that looks like this:

Observable.just("One", "Two", "Three")        .doOnNext(i -> log("doOnNext", i))        .observeOn(Schedulers.newThread())        .doOnNext(i -> log("doOnNext", i))        .subscribe(i -> log("subscribe", i));

Its structure can be represented in the following figure:

The output of such a call will be as follows:

doOnNext:main:OnedoOnNext:main:TwodoOnNext:main:ThreedoOnNext:RxNewThreadScheduler-1:Onesubscribe:RxNewThreadScheduler-1:OnedoOnNext:RxNewThreadScheduler-1:Two ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required