位图下载期间的UndeliverableException:RxJava

时间:2018-05-03 09:55:01

标签: android rx-java rx-java2

我的片段中有如下可观察链。我有一个微调器,触发项目选择Subject,根据选择,我使用Picasso在Schedulers.computation()下载位图。

Disposable d =  mSpinnerSelectionSubject.switchMap(selectedItem -> Observable.fromCallable(this::getImageDetails))
          .observeOn(Schedulers.computation())
          .switchMap(imageDetails -> Observable.fromCallable(() -> mPicasso.load(imageDetails.getImagePath()).get()))
          .observeOn(AndroidSchedulers.mainThread())
          .switchMap(this::setBitmapInView)
          .observeOn(Schedulers.computation())
          .switchMap(this::generateBitmaps) // this method does some opencv bitmap operation
          .observeOn(AndroidSchedulers.mainThread())
          .subscribeWith(new DisposableObserver<ImageData>() {
              @Override
              public void onNext(ImageData imageData) {
                  // do something
              }

              @Override
              public void onError(Throwable e) {
                  // handle error
              }

              @Override
              public void onComplete() {
                  Timber.d("onComplete: ");
              }
          }));

mCompositeDisposable.add(d);

它的工作正常,但有时我得到UndeliverableException。我已经给出了下面的堆栈跟踪。

io.reactivex.exceptions.UndeliverableException: java.io.InterruptedIOException: thread interrupted
    at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367)
    at io.reactivex.internal.operators.observable.ObservableFromCallable.subscribeActual(ObservableFromCallable.java:48)
    at io.reactivex.Observable.subscribe(Observable.java:11442)
    at io.reactivex.internal.operators.observable.ObservableSwitchMap$SwitchMapObserver.onNext(ObservableSwitchMap.java:126)
    at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:200)
    at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:252)
    at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:66)
    at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:57)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:761)
 Caused by: java.io.InterruptedIOException: thread interrupted
    at okio.Timeout.throwIfReached(Timeout.java:145)
    at okio.Okio$1.write(Okio.java:76)
    at okio.AsyncTimeout$1.write(AsyncTimeout.java:180)
    at okio.RealBufferedSink.flush(RealBufferedSink.java:224)
    at okhttp3.internal.http1.Http1Codec.finishRequest(Http1Codec.java:166)
    at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:84)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
    at okhttp3.RealCall.execute(RealCall.java:77)
    at com.squareup.picasso.OkHttp3Downloader.load(OkHttp3Downloader.java:91)
    at com.squareup.picasso.NetworkRequestHandler.load(NetworkRequestHandler.java:46)
    at com.squareup.picasso.BitmapHunter.hunt(BitmapHunter.java:219)
    at com.squareup.picasso.RequestCreator.get(RequestCreator.java:429)
    at com.my.app.modules.detailscreen.DetailFragment.lambda$null$7$DetailFragment(Fragment.java:117)

我尝试重新生成错误,我能看到的是,它主要发生在我下载位图时留下片段。

可观察的我添加到CompositeDisposable和片段&#39; onDestroyView&#39;事件我正在呼唤

mCompositeDisposable.clear();                

同样在onDestroy事件中我正在做

mCompositeDisposable.clear();                

我没有得到究竟导致崩溃的原因。希望有人可以帮助我。在此先感谢!!

1 个答案:

答案 0 :(得分:1)

例外的来源是Observable.fromCallable(() -> mPicasso.load(imageDetails.getImagePath()).get())。之所以发生这种情况,是因为它运行的调度程序在处理时发送Thread.interrupt()信号,fromCallable运算符将捕获异常并发出信号,无论操作是否已被处理。

要解决此问题,您可以使用Single.create代替,然后通过SingleEmitter.tryOnError方法手动捕获和处理异常。