我的片段中有一个可观察的链,如下所示。我有一个微调器,它会在项目选择上触发一个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。我在下面给出了stacktrace。

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并在片段“ onDestroyView”事件中调用的可观察对象

mCompositeDisposable.clear();


同样在onDestroy事件中我正在做

mCompositeDisposable.clear();


我没有真正导致崩溃的原因。希望有人能帮助我。提前致谢!!

最佳答案

例外的来源是Observable.fromCallable(() -> mPicasso.load(imageDetails.getImagePath()).get())。发生这种情况是因为正在运行的调度程序在被处置时会发送Thread.interrupt()信号,并且fromCallable运算符将捕获该异常并向其发出信号,而不管该操作是否已经被处置。

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

关于android - 下载位图期间出现UndeliverableException:RxJava,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50152106/

10-11 14:30