RxAndroid: Can't use AndroidSchedulers.mainThread() after migrating to RxJava2?

After migrating to RxJava2. observeOn doesn’t accept AndroidSchedulers. Is there a alternate in Schedulers for AndroidSchedulers.mainThread() or how should i solve this issue ?

OLD versions RxAndroid - 1.2.1 RxJava - 1.1.6

Migrated Versions RxAndroid - 2.0.1 RxJava - 2.0.3

 rxHelper.manageSubscription(
                mDataManager.getProducts()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new SingleSubscriber<List<Items>>() {
                    @Override
                    public void onSuccess(List<Items> products) {
                        getView().hideProgress();
                        if (!products.isEmpty()) {
                            getView().showText(String.valueOf(products.get(0).getName()));
                            getView().showItems(products);
                        } else {
                            getView().showEmpty();
                        }
                    }

                    @Override
                    public void onError(Throwable error) {
                        Timber.e(error, "There was an error retrieving the products");
                        getView().hideProgress();
                        getView().showError();
                    }
                })
        );

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 18 (6 by maintainers)

Most upvoted comments

Well, except for 2.0.0! But yeah they’re otherwise completely independently versioned libraries.

On Tue, Sep 19, 2017 at 3:46 PM Jake Wharton jakewharton@gmail.com wrote:

“don’t always” --> “never”

On Tue, Sep 19, 2017 at 3:46 PM Jason Atwood notifications@github.com wrote:

Be careful with

rxjava2_version = ‘2.0.1’ compile “io.reactivex.rxjava2:rxjava:$rxjava2_version” compile “io.reactivex.rxjava2:rxandroid:$rxjava2_version”

RxJava2 and RxAndroid(2) don’t always deploy updates in-sync (e.g. right now latest RxJava is 2.1.3 but latest RxAndroid is 2.0.1)

— You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub https://github.com/ReactiveX/RxAndroid/issues/357#issuecomment-330652577, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEdyROnLx2f-0D-0dPdMHZZXwmIB_ks5skBn9gaJpZM4LYsLB .

“don’t always” --> “never”

On Tue, Sep 19, 2017 at 3:46 PM Jason Atwood notifications@github.com wrote:

Be careful with

rxjava2_version = ‘2.0.1’ compile “io.reactivex.rxjava2:rxjava:$rxjava2_version” compile “io.reactivex.rxjava2:rxandroid:$rxjava2_version”

RxJava2 and RxAndroid(2) don’t always deploy updates in-sync (e.g. right now latest RxJava is 2.1.3 but latest RxAndroid is 2.0.1)

— You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub https://github.com/ReactiveX/RxAndroid/issues/357#issuecomment-330652577, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEdyROnLx2f-0D-0dPdMHZZXwmIB_ks5skBn9gaJpZM4LYsLB .

I’m already using 2.x branch. These are my dependencies.

    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'io.reactivex.rxjava2:rxjava:2.0.3'

rx

same error compile “io.reactivex.rxjava2:rxjava:2.1.1” compile ‘io.reactivex.rxjava2:rxandroid:2.0.1’

bugs gone use import io.reactivex.Observable

Go back to

Old Versions

compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.1.6'

Then to the Newer Version:

compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.1.1'

Sounds Wierd but it works

Yep. You’re importing the wrong class as Artem said. Correct the import or use the fully qualified name to point at the 2.x version of this class.

This version worked for me

implementation "io.reactivex.rxjava3:rxjava:3.0.0-RC3"
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'

the same error: compile 'io.reactivex.rxjava2:rxjava:2.1.1' compile 'io.reactivex.rxjava2:rxandroid:2.0.1' try compile 'io.reactivex.rxjava2:rxjava:2.1.0

This is working
// RX rxjava2_version = ‘2.0.1’ compile “io.reactivex.rxjava2:rxjava:$rxjava2_version” compile “io.reactivex.rxjava2:rxandroid:$rxjava2_version”

and sample code is

import statements are

        import io.reactivex.Observable;
        import io.reactivex.android.schedulers.AndroidSchedulers;
        import io.reactivex.disposables.Disposable;
        import io.reactivex.functions.Function;
        import io.reactivex.schedulers.Schedulers;

Sample Async

       private void applyDiscount(String order){
      Observable.just(order)
            .map(new Function<String, Boolean>() {
                @Override
                public Boolean apply(String order) throws Exception {
                    return null;
                }
            })
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new io.reactivex.Observer<Boolean>() {
                @Override
                public void onSubscribe(Disposable d) {

                }

                @Override
                public void onNext(Boolean value) {

                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            });
          }

Try

compile ‘io.reactivex:rxandroid:1.2.1’ compile ‘io.reactivex:rxjava:1.1.6’

Something else could still import RxJava 1 and you use that API. RxJava 1 and 2 are not binary compatible and v2 is not a drop-in replacement. You have to update your imports and change certain class and method names. See the wiki for futher details.