New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FlatMap and subscribeOn #2925
Comments
I don't understand this code as it doesn't compile as it is pasted. Here is my attempt to refactor it to work: import rx.Observable;
import rx.schedulers.Schedulers;
public class FlatMapSubscribeOn {
public static void main(String... args) {
Observable.just(1)
.flatMap(i -> {
Timber.d("FLATMAP PRE-1: %d - %s", Thread.currentThread());
return Observable.create(s -> {
Timber.d("FLATMAP PRE-1-CALL: %d - %s", Thread.currentThread());
s.onNext(i);
s.onCompleted();
});
})
.subscribeOn(Schedulers.io())
.flatMap(i -> {
Timber.d("FLATMAP POST-1: %d - %s", Thread.currentThread());
return Observable.create(s -> {
Timber.d("FLATMAP POST-1-CALL: %d - %s", Thread.currentThread());
s.onNext(i);
s.onCompleted();
});
}).toBlocking().forEach(i -> {
Timber.d("Subscribe: Thread: %d - %s", Thread.currentThread());
});
;
}
public static class Timber {
public static void d(String log, Thread thread) {
System.out.printf(log + "\n", thread.getId(), thread.getName());
}
}
} This outputs the following:
|
Exactly what i meant. Isn't it suppoused to run flatMap Observable code within another thread? Did i misunderstand something? |
So ie:
|
@dwursteisen thanks for your explanation I found this very helpful |
@dwursteisen does it change anything is if I move the .subscribeOn in your example just above the .subscribe call? |
It won't change anything. Le mer. 20 mai 2015 11:11, Andrea Baccega notifications@github.com a
|
Thanks @dwursteisen for the explanation |
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 |
Hello there. Consider this observable
This will emit the following:
This means that it subscribeOn is "lost" when flatMap is called?
The text was updated successfully, but these errors were encountered: