Cancelling an Observable in RxJava

前端 未结 3 453
死守一世寂寞
死守一世寂寞 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:12

    In my case I had a Publish subject that was emitting items in a certain condition. In some condition I wanted to cancel it, So I added a sealed class that holds the state:

    sealed class DelayedItem {
       object Ignore : DelayedItem()
       data class Handle(val text: String) : DelayedItem()
    }
    

    And my subscriber was bounced like that:

    myPublishSubject
                .subscribeOn(Schedulers.io())
                .debounce(500, TimeUnit.MILLISECONDS)
                .subscribe {
                    if (it is DelayedItem.Handle)
                        handleItem(it)
                }
    

    Hope it helps!

提交回复
热议问题