question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Does subscribeOn call order matter?

See original GitHub issue

Hi, I am using RxJava 2.1.1. I thought I knew how subscribeOn works, but sometimes I still have some doubts about it.

As far as I know, for subscribeOn it doesn’t metter the position where you use it (in the chain).

Assuming that I am executing this code from my MAIN THREAD.

Observable.just("")
                .doOnSubscribe(/* NEW THREAD */)
                .subscribeOn(Schedulers.newThread())
                .subscribe(/* NEW THREAD */);

As expected the doOnSubscribe() is called on the NEW THREAD, because of subscribeOn(Schedulers.newThread())

But, if I call subscribeOn before doOnSubscribe the result will be different:

Observable.just("")
                .subscribeOn(Schedulers.newThread())
                .doOnSubscribe(/* MAIN THREAD */)
                .subscribe(/* NEW THREAD */);

Now the doOnSubscirbe() seems to be executed in the MAIN THREAD, regardless of subscribeOn(Schedulers.newThread()).

Why is this happening?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:17
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

19reactions
akarnokdcommented, Jul 1, 2017

subscribeOn moves the subscription side-effects of the upstream to another thread but doesn’t affect the downstream’s subscription side-effects. Therefore, it matters were you put it if you have subscription side-effecting operators before it.

Let’s walk through what happens in the first case. The chain consist of Just -> DoOnSubscribe -> SubscribeOn -> LambdaObserver operators but the subscription process walks in reverse:

Step Thread Action
1 main Creation of LambdaObserver
2 main SubscribeOn.subscribe(LambdaObserver)
3 main SubscribeOn calls onSubscribe on LambdaObserver
4 main SubscribeOn schedules a subscribe action on newThread
5 newthread DoOnSubscribe.subscribe(SubscribeOnObserver)
6 newthread Just.subscribe(DoOnSubscribeObserver)
7 newthread Just calls onSubscribe on DoOnSubscribeObserver
8 newthread DoOnSubscribe calls the lambda and then onSubscribe on SubscribeOnObserver
9 newthread Since SubscribeOnObserver already called onSubscribe, the call returns to Just
10 newthread Just emits onNext

Now if doOnSubscribe is after subscribeOn:

Step Thread Action
1 main Creation of LambdaObserver
2 main DoOnSubscribe.subscribe(SubscribeOnObserver)
3 main SubscribeOn.subscribe(DoOnSubscribeObserver)
4 main SubscribeOn calls onSubscribe on DoOnSubscribeObserver
5 main DoOnSubscribe calls the lambda and then onSubscribe on LambdaObserver
6 main SubscribeOn schedules a subscribe action on newThread
7 newthread Just.subscribe(SubscribeOnObserver)
8 newthread Just calls onSubscribe on SubscribeOnObserver
9 newthread Since SubscribeOnObserver already called onSubscribe, the call returns to Just
10 newthread Just emits onNext
0reactions
leinli03commented, Jul 15, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Does the order of subscribeOn and observeOn matter?
Where you call subscribeOn() in a chain doesn't really matter. Where you call observeOn() does matter. subscribeOn() tells the whole chain ...
Read more >
Understanding RxJava subscribeOn and observeOn
It does not matter where you put the subscribeOn () in your Observable chain of operators.
Read more >
SubscribeOn operator - ReactiveX
As shown in this illustration, the SubscribeOn operator designates which thread the Observable will begin operating on, no matter at what point in...
Read more >
RxSwift: subscribeOn vs observeOn? | by Aaina jain | Swift India
Order doesn't matter, subscribeOn can be placed anywhere. observeOn: It allows you to change the scheduler on which the observer code will be...
Read more >
SubscribeOn and ObserveOn - Advanced Reactive Java
SubscribeOn · The purpose of subscribeOn() is to make sure side-effects from calling subscribe() happens on some other thread. · Why would one ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found