【发布时间】:2020-03-28 20:09:43
【问题描述】:
我的设置如下
活动:
onCreate(Bundle savedInstanceState) {
myViewModel.getData(intent.getParam).observe(this, this::dataDisplay);
}
视图模型
getData(String param) {
return LiveDataReactiveStreams.fromPublisher(repository.getData(param)
.toFlowable());
}
存储库:
getData(String param) {
return Single.fromCallable(() -> request.execute().fromJson())
.doOnSuccess(this::cache)
.subscribeOn(Schedulers.IO);
}
问题是,由于某种原因,一旦我的活动被创建,它就会被销毁并重新创建。因此onCreate 被调用了两次。 Activity 关闭,导致 livedata 向上发送 dispose 信号,并以某种方式导致下载线程中断,然后引发错误
io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | com.google.gson.JsonIOException: java.io.InterruptedIOException: thread interrupted
at io.reactivex.plugins.RxJavaPlugins.onError(RxJavaPlugins.java:367)
at io.reactivex.internal.operators.single.SingleFromCallable.subscribeActual(SingleFromCallable.java:50)
Caused by: java.io.InterruptedIOException: thread interrupted
at com.android.okhttp.okio.Timeout.throwIfReached(Timeout.java:145)
at com.android.okhttp.okio.Okio$2.read(Okio.java:133)
at com.android.okhttp.okio.AsyncTimeout$2.read(AsyncTimeout.java:211)
at com.android.okhttp.okio.RealBufferedSource.read(RealBufferedSource.java:50)
at com.android.okhttp.internal.http.Http1xStream$ChunkedSource.read(Http1xStream.java:439)
at com.android.okhttp.okio.RealBufferedSource$1.read(RealBufferedSource.java:371)
at com.google.api.client.http.javanet.NetHttpResponse$SizeValidatingInputStream.read(NetHttpResponse.java:164)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:288)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:351)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:180)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at com.google.gson.stream.JsonReader.fillBuffer(JsonReader.java:1291)
at com.google.gson.stream.JsonReader.nextQuotedValue(JsonReader.java:1031)
at com.google.gson.stream.JsonReader.nextString(JsonReader.java:816)
有没有人遇到过这个问题并有前进的方向?
【问题讨论】:
-
在
request.execute().fromJson()和this::cache方法中发布您拥有的代码。您可能违反了响应式流的规则,即生产者负责在取消流时不发出项目。 -
request.execute 执行 http 调用,
fromJson将来自 http 调用的响应反序列化为对象。这些是非反应式方法,使它们反应式的桥梁是fromCallable不是生产者在流被取消后发出项目,而是流被取消,然后抛出异常,因为流被取消。
标签: multithreading rx-java rx-java2 android-livedata