如何取消订阅RxJava2中的可观察或订阅?或者我真的需要吗?
问题描述:
如何取消订阅RxJava2中的可观察或订阅?或者我真的需要吗?如何取消订阅RxJava2中的可观察或订阅?或者我真的需要吗?
//Initialising Obersvable
Observable<SearchSuggestions> observable = NetworkContext.api.getSearch("");
//subscribing obersvable on another thread and observing on main thread
observable.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<SearchSuggestions>() {
@Override
public void accept(SearchSuggestions searchSuggestions) throws Exception {
// here I receive the fetched results
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
//here the error
}
});
答
要取消订阅用户,也许你应该申报
要取消订阅只需拨打任何时间,但我会建议要取消订阅的onDestroy(活动)或onDestroyView(片段)
if (subscription != null && subscription.isUnsubscribed()) {
subscription.unsubscribe();
}
+0
它不在rxJava2中 –
请阅读[this](https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#subscriber)。 – akarnokd