Skip to content

Instantly share code, notes, and snippets.

@jiangecho
Forked from lapastillaroja/README.md
Created December 26, 2015 09:47
Show Gist options
  • Save jiangecho/646a91a27e705a24bfd5 to your computer and use it in GitHub Desktop.
Save jiangecho/646a91a27e705a24bfd5 to your computer and use it in GitHub Desktop.
Don't break the chain: use RxJava's compose() operator -> Android + Retrolambda + Observable.Transformer cannot find symbol method

Problem

Error:(29, 37) error: cannot find symbol method subscribeOn(Scheduler)

This happens compiling this code borrowed from http://blog.danlew.net/2015/03/02/dont-break-the-chain/#comment-2388677116

final Observable.Transformer schedulersTransformer =
            observable -> observable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());

Environment

  • Android Studio 2.0
  • Retrolambda
  • Compiled with Java 8
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
public class RxUtil {
/**
* {@link rx.Observable.Transformer} that transforms the source observable to subscribe in the
* io thread and observe on the Android's UI thread.
*/
private static Observable.Transformer ioToMainThreadSchedulerTransformer;
static {
ioToMainThreadSchedulerTransformer = createIOToMainThreadScheduler();
}
/**
* Get {@link rx.Observable.Transformer} that transforms the source observable to subscribe in
* the io thread and observe on the Android's UI thread.
*
* Because it doesn't interact with the emitted items it's safe ignore the unchecked casts.
*
* @return {@link rx.Observable.Transformer}
*/
@SuppressWarnings("unchecked")
private static <T> Observable.Transformer<T, T> createIOToMainThreadScheduler() {
return tObservable -> tObservable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
@SuppressWarnings("unchecked")
public static <T> Observable.Transformer<T, T> applyIOToMainThreadSchedulers() {
return ioToMainThreadSchedulerTransformer;
}
}
ApiService.getInstance().apiCall(parameter)
.compose(RxUtil.applyIOToMainThreadSchedulers())
.subscribe(data -> {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment