Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxAndroid 3 main thread

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?

like image 341
Duna Avatar asked Oct 16 '19 13:10

Duna


2 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

like image 116
vs.thaakur Avatar answered Oct 22 '22 21:10

vs.thaakur


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;
like image 29
user1297133 Avatar answered Oct 22 '22 22:10

user1297133