RxAndroid 3 main thread

Viewed 5653

I am trying to find the main thread for subscribeOn in Rx3

Single.just(getHeavyData())
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Consumer<Data>() {
                                    @Override
                                    public void accept(Data d) throws Throwable {
                                        setAdapters(d);
                                    }
                                });

AndroidSchedulers.mainThread() - is not compatible with the brand new RX3

Gradle import: implementation "io.reactivex.rxjava3:rxjava:3.0.0-RC3"

How can we find the main thread in order to do changes to the UI?

4 Answers

AndroidSchedulers.mainThread() is not part of Rx Java 1,2 or 3. Its the part of RxAndroid Library. Add RxAndroid dependency to your project and you will get this method.

RxAndroid still uses RxJava2. Until there is an update from the creators of the library this problem remains.

The new package structure has been released with 3.0.0-RC2 and there is a support library so that v2 and v3 can talk to each other without hidden or overt compilation/runtime problems from before. This also means that module override tricks no longer work so you have to bridge AndroidSchedulers manually or convert from v2 sources used in Retrofit until these (and many other) libraries start supporting v3.

Please refer this and this

This is an issue with imports. I had a similar problem before realizing that I need to reference RxJava3. Use these imports for Schedulers and Disposable Single Observers


    import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
    import io.reactivex.rxjava3.observers.DisposableSingleObserver;
    import io.reactivex.rxjava3.schedulers.Schedulers;

Also, if you have a service with a method that returns a Single use

    import io.reactivex.rxjava3.core.Single;

Had a similar problem and the compatibility issue was from an import of the Single. Even though I was using RxJava3 library and RxAndroid3, I was still using:

import io.reactivex.Single

But was resolved when pointing the Single to:

import io.reactivex.rxjava3.core.Single

Check your import, maybe you are importing something like:

implementation 'io.reactivex.**rxjava2**:rxandroid:x.y.z'

but you should be importing something like:

implementation 'io.reactivex.**rxjava3**:rxandroid:3.0.0' (check the last version)

that solved my problem

Related