Skip to content

Instantly share code, notes, and snippets.

@lapastillaroja
Last active March 14, 2017 11:41
Show Gist options
  • Save lapastillaroja/695d18ed75d7c201f6c4 to your computer and use it in GitHub Desktop.
Save lapastillaroja/695d18ed75d7c201f6c4 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 -> {});
@sourabhv
Copy link

Observable.Transformer<Observable, Observable> schedulersTransformer = observable -> observable.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment