处理后调用 Rx Observable onCompleted [英] Rx Observable onCompleted called after disposed
问题描述
我在我的 Android 应用中使用 RxJava2.我有一个 Observable
,它在创建时被添加到 CompositeDisposable
.这个 Observable
正在从一些 API 检索数据,它可能需要很长时间才能完成.
I`m using RxJava2 in my android app. I have an Observable
that, when created, is added to a CompositeDisposable
. This Observable
is retrieving data from some APIs and it can take pretty long to finish.
当我在下载时离开页面时,在 onPause
中,我正在处理这个 CompositeDisposable
,但有时 Observable
的onComplete
仍然被调用.
When I`m leaving the page while downloading, in onPause
, I`m disposing this CompositeDisposable
, but sometimes the Observable
`s onComplete
is still called.
怎么会这样?
推荐答案
如 Doc 中所述,onTerminateDetach
将上游生产者和下游的引用归零.
As mentioned in Doc,onTerminateDetach
nulls out references to the upstream producer and downstream.
例如
mDisposable.add(
apiClient.apiCallSomeAPI()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.concatMap(this::responseObservable)
.toList()
.onTerminateDetach() // this is important, exactly before subscribe
.doOnSuccess(this::realmBulkOperation)
.subscribe(this::handleResult, this::handleError)
);
这篇关于处理后调用 Rx Observable onCompleted的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!