Does subscribeOn call order matter?
See original GitHub issueHi, 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:
- Created 6 years ago
- Reactions:17
- Comments:7 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
LambdaObserver
SubscribeOn.subscribe(LambdaObserver)
SubscribeOn
callsonSubscribe
onLambdaObserver
SubscribeOn
schedules a subscribe action onnewThread
DoOnSubscribe.subscribe(SubscribeOnObserver)
Just.subscribe(DoOnSubscribeObserver)
Just
callsonSubscribe
onDoOnSubscribeObserver
DoOnSubscribe
calls the lambda and thenonSubscribe
onSubscribeOnObserver
SubscribeOnObserver
already calledonSubscribe
, the call returns toJust
Just
emitsonNext
Now if
doOnSubscribe
is aftersubscribeOn
:LambdaObserver
DoOnSubscribe.subscribe(SubscribeOnObserver)
SubscribeOn.subscribe(DoOnSubscribeObserver)
SubscribeOn
callsonSubscribe
onDoOnSubscribeObserver
DoOnSubscribe
calls the lambda and thenonSubscribe
onLambdaObserver
SubscribeOn
schedules a subscribe action onnewThread
Just.subscribe(SubscribeOnObserver)
Just
callsonSubscribe
onSubscribeOnObserver
SubscribeOnObserver
already calledonSubscribe
, the call returns toJust
Just
emitsonNext
https://stackoverflow.com/questions/50075574/doonsubscribe-gets-called-on-main-thread the answer here seems not quite match the explaintion