在后台线程而不是主线程上调用SingleObserver.onError

时间:2019-07-09 07:43:57

标签: rx-java2 reactivex

正如我从使用singel.observeOn(Scheduler)的文档中了解到的那样,将保证任何下游事件都将在该调度程序上执行。

显然,在我收到此错误时,在引发该错误的同一调度程序上调用了onError-

> Caused by:java.lang.IllegalStateException: Cannot invoke setValue on a background thread
at androidx.lifecycle.LiveData.assertMainThread(LiveData.java:443)
at androidx.lifecycle.LiveData.setValue(LiveData.java:286)
at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:33)
at com.bonimoo.womlauncher.presentation.wizard.registration.RegistrationViewModel$1.onError(RegistrationViewModel.java:89)
at io.reactivex.internal.operators.single.SingleFlatMap$SingleFlatMapCallback$FlatMapSingleObserver.onError(SingleFlatMap.java:116)
at io.reactivex.internal.observers.ResumeSingleObserver.onError(ResumeSingleObserver.java:51)
at io.reactivex.internal.disposables.EmptyDisposable.error(EmptyDisposable.java:78)
at io.reactivex.internal.operators.single.SingleError.subscribeActual(SingleError.java:42)
at io.reactivex.Single.subscribe(Single.java:3603)

此代码之外-

public static<T> SingleTransformer<T,T> getSingleTransformer(){
return upstream -> upstream
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread());
}


GetHotelsList getHotelsList = new GetHotelsList(
    AsyncTransformers.getSingleTransformer(), networkRepo);

getHotelsList.getHotels()
   .map(hotels->   
        CollectionsUtil.mapList(hotels,
        RegistrationMappers::mapHotelToPresentationHotel)
   )
   .flatMap((Function<List<HotelPresentation>, SingleSource<List<HotelPresentation>>>) hotelPresentations -> 
        Completable.timer(5,TimeUnit.SECONDS, Schedulers.io())
        .andThen(Single.error(new Throwable()))
   )
   .subscribe(new SingleObserver<List<HotelPresentation>>() {
            @Override
            public void onSubscribe(Disposable d) {
                addDisposable(d);
                RegistrationState currentState = stateLiveData.getValue();
                stateLiveData.setValue(currentState.newBuilder().setLoadingHotels(true).build());
            }
            @Override
            public void onSuccess(List<HotelPresentation> hotelPresentations) {
                RegistrationState currentState = stateLiveData.getValue();
                stateLiveData.setValue(currentState.newBuilder().setHotelsList(hotelPresentations).setLoadingHotels(false).build());
            }
            @Override
            public void onError(Throwable e) {
                RegistrationState currentState = stateLiveData.getValue();
                stateLiveData.setValue(currentState.newBuilder().setLoadingHotels(false).build());
            }
        });

并且调试器显示onError在RxSchedulerIoThread上调用。

0 个答案:

没有答案