【发布时间】:2016-03-15 23:07:10
【问题描述】:
我正在使用 Retrofit 2.0 和 Rx-android 来加载我的 API。我在this site 关注RxJava Integration with CallAdapter 部分,它工作正常。但是,我不知道如何使用可观察对象取消加载请求。请帮忙给我一个想法。
【问题讨论】:
标签: android retrofit rx-java rx-android
我正在使用 Retrofit 2.0 和 Rx-android 来加载我的 API。我在this site 关注RxJava Integration with CallAdapter 部分,它工作正常。但是,我不知道如何使用可观察对象取消加载请求。请帮忙给我一个想法。
【问题讨论】:
标签: android retrofit rx-java rx-android
取消 RxJava Observable 执行的唯一方法 - 取消订阅。 RxJavaCallAdapter 会将取消委托给 okhttp 客户端。
那么,你就这么干吧:
Subscription subscription = getObservable().subscribe();
//When you want to stop execution
subscription.unsubsribe();
您可以查看代码here。具体是这些代码行
final Call<T> call = originalCall.clone();
// Attempt to cancel the call if it is still in-flight on unsubscription.
subscriber.add(Subscriptions.create(new Action0() {
@Override public void call() {
call.cancel();
}
}));
【讨论】: