Schedulers.io () does not return to the main thread

I use RxParse to parse the request asynchronously, but when I sign my observables using subscribeOn (Schedulers.io ()), my onCompleted method is never called on the main thread. Instead, my onCompleted method is called inside the workflow pool. If I use the watch function (AndroidSchedulers.mainThread), everything will work, but my onNextMethod will be called in the main thread too, and I don't want this.

Is there something wrong in my code?

Is there something wrong in my code?

ParseObservable.find(myQuery)
    .map(myMapFunc())
    .subscribeOn(AndroidSchedulers.handlerThread(new Handler()))
    .subscribe(
        new Subscriber<MyObj>() {
           @Override
            public void onError(Throwable e) {
                Log.e("error","error",e);
            }

            @Override
            public void onNext(T t) {
                // ... worker thread (but here is ok)
            }

            public void onCompleted() {
                // ... worker thread again instead of mainThread
            }
        }
    )
);
+4
source share
4 answers

, (onNext, onError onCompleted

Schedulers.io() onNext(T t), Observable MainThread :

ParseObservable.find(myQuery)
    .map(myMapFunc())
    .subscribeOn(Schedulers.io())
    .subscribe(
        new Subscriber<MyObj>() {
           @Override
            public void onError(Throwable e) {
                Log.e("error","error",e);
            }

            @Override
            public void onNext(T t) {
                Observable.just(t)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe((t) -> {
                         // do something in MainThread
                    })
            }

            public void onCompleted() {
                // ... worker thread again instead of mainThread
            }
        }
    )
);

, !

+5

subscribeOn() observeOn(). , Rx.

subscribeOn() , . onNext(), onError() onComplete().

observeOn() , (, onNext()). , Observable .

. , , API- RxJava. , , .

+8

" ". , :

ParseObservable.find(myQuery)
        .map(myMapFunc())
        .subscribeOn(AndroidSchedulers.handlerThread(new Handler()))
        .doOnCompleted(() -> onCompleteAction())
        .observeOn(AndroidSchedulers.mainThread())
        .doOnNext(value -> onNext(value))
        .subscribe();
+2

.

subscribeOn , Observable , .

observeOncan be used at different points (and many times, if necessary) in your observed chain to transfer control between threads. (You can check this by checking if you are in the main thread or not in each of these blocks).

ParseObservable.find(myQuery)
    .map(myMapFunc())

    // Added this:
    .doOnNext(obj -> {
        // NOTE: This will happen on your `subscribeOn` scheduler 
        // Do something with `obj` here while on worker thread
    }

    .subscribeOn(AndroidSchedulers.handlerThread(new Handler()))

    // Added this:
    .observeOn(AndroidSchedulers.mainThread())    

    .subscribe(new Subscriber<>() {
        next -> {
            // NOTE: This will happen on the main thread
        },
        error -> {
            Log.e("error","error",e);
            // NOTE: This will happen on the main thread
        },
        () -> {
            // NOTE: This will happen on the main thread
        }
    });
+2
source

Source: https://habr.com/ru/post/1619945/


All Articles