在 RxJava 中取消 Observable [英] Cancelling an Observable in RxJava

查看:43
本文介绍了在 RxJava 中取消 Observable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在执行下载的 observable.但是,当我点击一个按钮时,我想取消那个 observable.

I have an observable that is performing a download. However I want to cancel that observable when I click on a button.

这怎么可能?

谢谢.

推荐答案

你不能真正告诉 observable 取消网络请求,因为 RxJava 不知道在 Observable 内部运行的代码.您可以做的是使用 doOnUnsubscribe 来检查网络请求是否仍在运行,如果取消,则在您想取消时取消订阅.

You can't really tell an observable to cancel a network request as RxJava has no knowledge of the code thats running inside of the Observable. What you can do is use the doOnUnsubscribe to check whether the network request is still running and if it is cancel it, then just unsubscribe when you want to cancel.

这篇关于在 RxJava 中取消 Observable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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