LiveData暂停会中断下载线程并导致崩溃

问题描述 投票:0回答:1

我的设置如下

活动:

onCreate(Bundle savedInstanceState) {
  myViewModel.getData(intent.getParam).observe(this, this::dataDisplay);
}

ViewModel

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被调用了两次。活动关闭,导致livedata发送处理信号,并以某种方式导致下载线程中断,然后引发错误

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)

有人遇到这个问题并且有前进的道路吗?

multithreading rx-java rx-java2 android-livedata
1个回答
0
投票

我最终要做的是创建Single.fromCallable的副本,如果取消订阅后在Callable中抛出异常,该副本不会崩溃。

© www.soinside.com 2019 - 2024. All rights reserved.