Cancelling an Observable in RxJava

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

    This should give you the general idea. The summary is to use Observable.using() and subscribe to the Observable with a Subscriber so you can call subscriber.unsubscribe() when the user clicks the button to cancel.

    Here's an outline.

    Subscriber<T> subscriber = ...;
    Observable
        // create a stream from a socket and dispose of socket
        // appropriately
        .using(socketCreator(host, port, quietTimeoutMs),
               socketObservableFactory(charset), 
               socketDisposer(), true)
        // cannot ask host to slow down so buffer on
        // backpressure
       .onBackpressureBuffer()
       .subscriber(subscriber);
    

    On clicking a button call:

    subscriber.unsubscribe()
    

    This will stop the observable stream and call socketDisposer() to stop the network activity.

    You haven't specified what is the nature of your download (ftp, http, etc) but the answer doesn't really change because all of those transports will fit this pattern.

    0 讨论(0)
  • 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!

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题