Reputation: 219
http://blog.danlew.net/2015/07/23/deferring-observable-code-until-subscription-in-rxjava/ discusses creating observables using Observable.just, Observable.create, and Observable.defer
Let's say I have this:
List<File> fileIOFunction() {
// does some file io operations
}
OBSERVABLE.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
What thread do the file io operations run on if OBSERVABLE is:
Observable.create(
new Observable.OnSubscribe<List<File>>() {
@Override
public void call(Subscriber<? super List<File>>
subscriber) {
subscriber.onNext(fileIOFunction());
}
If OBSERVABLE is Observable.just(fileIOFunction())
If OBSERVABLE is
Observable.defer(new Func0<Observable<List<File>>>() {
@Override
public Observable<List<File>> call() {
return Observable.just(fileIOFunction());
});
Upvotes: 0
Views: 1261
Reputation: 4012
For just it will run on calling thread, because fileIOFunction() will be invoked eagerly. Defer and Create will run on Schedulers.io() due to subscribeOn and will switch to AndroidSchedulers.mainThread() due to observeOn (switch thread). Create and Defer are lazy.
Upvotes: 1