Cancelling an Observable in RxJava

前端 未结 3 431
死守一世寂寞
死守一世寂寞 2021-01-18 20:24

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

How is that possible?

Thank you.

3条回答
  •  再見小時候
    2021-01-18 21:19

    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.

提交回复
热议问题