问题描述
我有休息用的api.
@Get("/serveraction")
public Observable<String> myRequest(@Query("Data") String data);
我知道okhttp具有取消功能(按请求对象,按标签),但是不知道如何在翻新和rxjava中使用它.用翻新和rxjava实现网络任务取消机制的最佳方法是什么?
I know, that okhttp has canceling functionality(by request object, by tag), but don't know how use it with retrofit and rxjava. What is the best way to realize canceling mechanism for network tasks with retrofit and rxjava?
推荐答案
您可以使用标准的RxJava2取消机制一次性用品.
You can use the standard RxJava2 cancelling mechanism Disposable.
Observable<String> o = retrofit.getObservable(..);
Disposable d = o.subscribe(...);
// later when not needed
d.dispose();
Retrofit RxJava调用适配器会将其重定向到okHttp的取消.
Retrofit RxJava call adapter will redirect this to okHttp's cancel.
RxJava2: https://github.com/square/retrofit/blob/46dc939a0dfb470b3f52edc88552f6f7ebb49f42/retrofit-adapters/rxjava2/src/main/java/retrofit2/adapter/rxjava2/CallEnqueueObservable.java#L92-L95
这篇关于如何使用改造和rxjava取消任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!