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.

FlatMap and subscribeOn

See original GitHub issue

Hello 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:closed
  • Created 8 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

33reactions
dwursteisencommented, May 4, 2015

subscribeOn will run the code which perform the subscription into the schedculer given in subscribeOn.

So Observable.just(...) will executed in the io thread pool. And all sub sequent call too, if you don’t give another Scheduler using observeOn.

ie:

Observable.just(1) // 1 will be emited in the IO thread pool
                  .subscribeOn(Schedulers.io())
                  .flatMap(...) // will be in the IO thread pool
                  .observeOn(Schedulers.computation())
                  .flatMap(...) // will be executed in the computation thread pool
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe(); // will be executed in the Android main thread (if you're running your code on Android)
4reactions
soshialcommented, Mar 14, 2019

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.:

    public void main() {
        Observable.just(1)
                  .subscribeOn(Schedulers.io())
                  .observeOn(Schedulers.io()) // this is put just for demonstration
                  .doOnNext(this::printThread) // IO
                  .flatMap(i -> incrementOnIoThread1(i)) // or incrementOnIoThread2(i)
                  .subscribe(this::printThread // computation
                             , throwable -> {});
    }

    public Observable<Integer> incrementOnIoThread1(int i) {
        return Observable.just(i + 1)
                .observeOn(Schedulers.computation());
    }

    public Observable<Integer> incrementOnIoThread2(int i) {
        return Observable.just(i + 1)
                .subscribeOn(Schedulers.computation());
    }

The only thing we can to alleviate this, is to add observeOn after flatMap, as Karnok said here.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

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