如何使用改造和rxjava取消任务 [英] How cancel task with retrofit and rxjava

查看:587
本文介绍了如何使用改造和rxjava取消任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有休息用的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.

RxJava1: RxJava2:

RxJava2: https://github.com/square/retrofit/blob/46dc939a0dfb470b3f52edc88552f6f7ebb49f42/retrofit-adapters/rxjava2/src/main/java/retrofit2/adapter/rxjava2/CallEnqueueObservable.java#L92-L95

这篇关于如何使用改造和rxjava取消任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆