FlatMap and subscribeOn
See original GitHub issueHello there. Consider this observable
Observable.just(1)
.flatMap( i -> {
Timber.d("FLATMAP PRE-1: %d - %s", Thread.currentThread().getId(), Thread.currentThread().getName());
return Observable.create(s -> {
Timber.d("FLATMAP PRE-1-CALL: %d - %s", Thread.currentThread().getId(), Thread.currentThread().getName());
s.onNext(i);
s.onCompleted();
})
.subscribeOn(Schedulers.io())
.flatMap( i -> {
Timber.d("FLATMAP POST-1: %d - %s", Thread.currentThread().getId(), Thread.currentThread().getName());
return Observable.create(s -> {
Timber.d("FLATMAP POST-1-CALL: %d - %s", Thread.currentThread().getId(), Thread.currentThread().getName());
s.onNext(i);
s.onCompleted();
})
.subscribe( i -> {
Timber.d("Subscribe: Thread: %d - %s", Thread.currentThread().getId(), Thread.currentThread().getName());
});
This will emit the following:
FLATMAP PRE-1: 1 - main
FLATMAP PRE-1-CALL: 1 - main
FLATMAP POST-1: 1 - main
FLATMAP POST-1-CALL: 1 - main
Subscribe: Thread: 1 - main
This means that it subscribeOn is “lost” when flatMap is called?
Issue Analytics
- State:
- Created 8 years ago
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Understanding RxJava subscribeOn and observeOn
flatMap () wraps each item being emitted by an Observable letting you apply its own RxJava operators including assigning a new Scheduler ...
Read more >How to make flatMap execute on background thread
Subscribing to my Observable: myLoader.getMyData() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new ...
Read more >Understanding Map, FlatMap, SwitchMap and ConcatMap | by ...
FlatMap and ConcatMap work are pretty much the same. ... SwitchMap is a bit different from FlatMap and ConcatMap. ... subscribeOn(Schedulers.io()).
Read more >How to Prevent Reactive Java Applications from Stalling
The mapper function passed to flatMap() transforms the input sequence into N ... overriding any the scheduler assigned with subscribeOn() .
Read more >Observable (RxJava Javadoc 3.1.5) - ReactiveX
It is recommended applying subscribeOn(Scheduler) to move this blocking wait to a background thread ... See Also: ReactiveX operators documentation: FlatMap ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
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
will run the code which perform the subscription into the schedculer given insubscribeOn
.So
Observable.just(...)
will executed in the io thread pool. And all sub sequent call too, if you don’t give anotherScheduler
usingobserveOn
.ie:
Important thing to note though, that if Observables inside flatMap would subscribe on some other thread, then these flatMaps would override initial thread. E.g.:
The only thing we can to alleviate this, is to add
observeOn
afterflatMap
, as Karnok said here.