我对RxJava onErrorResumeNext运算符有问题。 我想获取位置,然后从服务器(使用Retrofit)获取数据取决于位置,但如果没有位置(错误:序列不包含元素),我想从服务器获取另一个Observable(不依赖于位置)的数据)。我尝试使用onErrorResumeNext运算符,但得到“java.io.InterruptedIOException:thread interrupted”。
添加onErrorResumeNext之前的代码 - 效果很好
LocationService.getUpdatedOrLastKnownLocation(getContext()))
.flatMap(location -> RestService.getPostsAround(location,0,10)) //offset = 0, limit = 10;
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
使用onErrorResumeNext的代码 - 抛出异常
LocationService.getUpdatedOrLastKnownLocation(getContext()))
.flatMap(location -> RestService.getPostsAround(location,0,10)) //offset = 0, limit = 10;
.onErrorResumeNext(RestService.getPostsByMapProjection(googleMap.getProjection().getVisibleRegion()))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
堆栈跟踪:
07-27 22:33:58.384 18632-18632/com.blacksea.plamobi W/System.err: java.io.InterruptedIOException: thread interrupted
07-27 22:33:58.384 18632-18632/com.blacksea.plamobi W/System.err: at okio.Timeout.throwIfReached(Timeout.java:145)
07-27 22:33:58.385 18632-18632/com.blacksea.plamobi W/System.err: at okio.Okio$1.write(Okio.java:77)
07-27 22:33:58.385 18632-18632/com.blacksea.plamobi W/System.err: at okio.AsyncTimeout$1.write(AsyncTimeout.java:155)
07-27 22:33:58.385 18632-18632/com.blacksea.plamobi W/System.err: at okio.RealBufferedSink.flush(RealBufferedSink.java:221)
07-27 22:33:58.386 18632-18632/com.blacksea.plamobi W/System.err: at okhttp3.internal.http.Http1xStream.finishRequest(Http1xStream.java:159)
07-27 22:33:58.386 18632-18632/com.blacksea.plamobi W/System.err: at okhttp3.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:721)
07-27 22:33:58.387 18632-18632/com.blacksea.plamobi W/System.err: at okhttp3.internal.http.HttpEngine.access$200(HttpEngine.java:81)
07-27 22:33:58.387 18632-18632/com.blacksea.plamobi W/System.err: at okhttp3.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:708)
07-27 22:33:58.389 18632-18632/com.blacksea.plamobi W/System.err: at okhttp3.internal.http.HttpEngine.readResponse(HttpEngine.java:563)
07-27 22:33:58.389 18632-18632/com.blacksea.plamobi W/System.err: at okhttp3.RealCall.getResponse(RealCall.java:241)
07-27 22:33:58.389 18632-18632/com.blacksea.plamobi W/System.err: at okhttp3.RealCall$ApplicationInterceptorChain.proceed(RealCall.java:198)
07-27 22:33:58.391 18632-18632/com.blacksea.plamobi W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:160)
07-27 22:33:58.392 18632-18632/com.blacksea.plamobi W/System.err: at okhttp3.RealCall.execute(RealCall.java:57)
07-27 22:33:58.392 18632-18632/com.blacksea.plamobi W/System.err: at retrofit2.OkHttpCall.execute(OkHttpCall.java:174)
07-27 22:33:58.392 18632-18632/com.blacksea.plamobi W/System.err: at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$RequestArbiter.request(RxJavaCallAdapterFactory.java:171)
07-27 22:33:58.392 18632-18632/com.blacksea.plamobi W/System.err: at rx.Subscriber.setProducer(Subscriber.java:211)
07-27 22:33:58.393 18632-18632/com.blacksea.plamobi W/System.err: at rx.Subscriber.setProducer(Subscriber.java:205)
07-27 22:33:58.394 18632-18632/com.blacksea.plamobi W/System.err: at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:152)
07-27 22:33:58.394 18632-18632/com.blacksea.plamobi W/System.err: at retrofit2.adapter.rxjava.RxJavaCallAdapterFactory$CallOnSubscribe.call(RxJavaCallAdapterFactory.java:138)
07-27 22:33:58.394 18632-18632/com.blacksea.plamobi W/System.err: at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:50)
07-27 22:33:58.395 18632-18632/com.blacksea.plamobi W/System.err: at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30)
07-27 22:33:58.395 18632-18632/com.blacksea.plamobi W/System.err: at rx.Observable.unsafeSubscribe(Observable.java:8460)
07-27 22:33:58.396 18632-18632/com.blacksea.plamobi W/System.err: at rx.internal.operators.OnSubscribeFlattenIterable.call(OnSubscribeFlattenIterable.java:65)
07-27 22:33:58.396 18632-18632/com.blacksea.plamobi W/System.err: at rx.internal.operators.OnSubscribeFlattenIterable.call(OnSubscribeFlattenIterable.java:37)
07-27 22:33:58.396 18632-18632/com.blacksea.plamobi W/System.err: at rx.Observable.unsafeSubscribe(Observable.java:8460)
07-27 22:33:58.398 18632-18632/com.blacksea.plamobi W/System.err: at rx.internal.operators.OperatorOnErrorResumeNextViaFunction$4.onError(OperatorOnErrorResumeNextViaFunction.java:141)
07-27 22:33:58.398 18632-18632/com.blacksea.plamobi W/System.err: at rx.internal.operators.OperatorMerge$MergeSubscriber.reportError(OperatorMerge.java:266)
07-27 22:33:58.398 18632-18632/com.blacksea.plamobi W/System.err: at rx.internal.operators.OperatorMerge$MergeSubscriber.checkTerminate(OperatorMerge.java:810)
07-27 22:33:58.400 18632-18632/com.blacksea.plamobi W/System.err: at rx.internal.operators.OperatorMerge$MergeSubscriber.emitLoop(OperatorMerge.java:571)
07-27 22:33:58.400 18632-18632/com.blacksea.plamobi W/System.err: at rx.internal.operators.OperatorMerge$MergeSubscriber.emit(OperatorMerge.java:560)
07-27 22:33:58.400 18632-18632/com.blacksea.plamobi W/System.err: at rx.internal.operators.OperatorMerge$MergeSubscriber.onError(OperatorMerge.java:276)
07-27 22:33:58.401 18632-18632/com.blacksea.plamobi W/System.err: at rx.internal.operators.OperatorMap$MapSubscriber.onError(OperatorMap.java:85)
07-27 22:33:58.403 18632-18632/com.blacksea.plamobi W/System.err: at rx.internal.operators.OperatorMerge$MergeSubscriber.reportError(OperatorMerge.java:266)
07-27 22:33:58.403 18632-18632/com.blacksea.plamobi W/System.err: at rx.internal.operators.OperatorMerge$MergeSubscriber.checkTerminate(OperatorMerge.java:810)
07-27 22:33:58.403 18632-18632/com.blacksea.plamobi W/System.err: at rx.internal.operators.OperatorMerge$MergeSubscriber.emitLoop(OperatorMerge.java:571)
07-27 22:33:58.404 18632-18632/com.blacksea.plamobi W/System.err: at rx.internal.operators.OperatorMerge$MergeSubscriber.emit(OperatorMerge.java:560)
07-27 22:33:58.404 18632-18632/com.blacksea.plamobi W/System.err: at rx.internal.operators.OperatorMerge$InnerSubscriber.onError(OperatorMerge.java:844)
07-27 22:33:58.404 18632-18632/com.blacksea.plamobi W/System.err: at rx.internal.operators.OperatorSubscribeOn$1$1.onError(OperatorSubscribeOn.java:59)
07-27 22:33:58.405 18632-18632/com.blacksea.plamobi W/System.err: at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.checkTerminated(OperatorObserveOn.java:264)
07-27 22:33:58.406 18632-18632/com.blacksea.plamobi W/System.err: at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:207)
07-27 22:33:58.406 18632-18632/com.blacksea.plamobi W/System.err: at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
07-27 22:33:58.406 18632-18632/com.blacksea.plamobi W/System.err: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
07-27 22:33:58.406 18632-18632/com.blacksea.plamobi W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-27 22:33:58.407 18632-18632/com.blacksea.plamobi W/System.err: at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
07-27 22:33:58.407 18632-18632/com.blacksea.plamobi W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
07-27 22:33:58.407 18632-18632/com.blacksea.plamobi W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
07-27 22:33:58.407 18632-18632/com.blacksea.plamobi W/System.err: at java.lang.Thread.run(Thread.java:818)
答案 0 :(得分:2)
根据我的经验,java.io.InterruptedIOException
是一个红色鲱鱼,因为它通常意味着某些其他线程取消订阅,并且正在运行的任何线程(fe网络请求)都会被中断。 / p>
在您的情况下,每次都会调用RestService.getPostsByMapProjection
是否存在错误,这可能不是您想要的行为;考虑将其包装在Observable.defer()
答案 1 :(得分:2)
如果使用了retrofit2,它的observable不会改变它们操作的线程,所以所有的链直到subscribeOn()在来自io scheduler的单线程上运行。显然来自RestService.getPostsAround
的错误为该线程设置了中断标志,而okio在RestService.getPostsByMapProjection
中对其进行了扼流。您可以尝试在subscribeOn(Schedulers.io())
中RestService.getPostsByMapProjection
之后添加onErrorResumeNext()
。
答案 2 :(得分:0)
您可以在flatMap上使用max concurrency,以确保您没有任何并发问题
@Beta
public final <R> Observable<R> flatMap(Func1<? super T, ? extends Observable<? extends R>> func, int maxConcurrent) {
if (getClass() == ScalarSynchronousObservable.class) {
return ((ScalarSynchronousObservable<T>)this).scalarFlatMap(func);
}
return merge(map(func), maxConcurrent);
}
在您的代码中
LocationService.getUpdatedOrLastKnownLocation(getContext()))
.flatMap(location -> RestService.getPostsAround(location,0,10),1) //offset = 0, limit = 10;
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);