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 -> {});
@azizbekian
Copy link

If you Replace lambda with anonymous class (ALT + ENTER in Intellij), you'll see this:

        @Override public Object call(Object observable) {
        return observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

And you'd see, that Object indeed has not subscribeOn() method. Casting observable to Observable and replacing with lambda gives this:

observable -> ((Observable) observable).subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());

Which compiles successfully.

@igoticecream
Copy link

Same configuration as you, this is working for me:

private final Observable.Transformer<Object, Object> mSchedulersTransformer;

Later...

mSchedulersTransformer = observable -> observable .subscribeOn(postThread.get()) .observeOn(mainThread.get());

Finally...

@SuppressWarnings("unchecked") protected <T> Transformer<T, T> applySchedulers() { return (Transformer<T, T>) mSchedulersTransformer; }

@jemshit
Copy link

jemshit commented Aug 19, 2016

I did ((Observable) observable).subscribeOn(Schedulers.io()) and it worked

@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