24

I want to cancel a request and one of the ways is to manually remove the disposable bag.

.addDisposableTo(disposeBag)

As I have the disposeBag object, is there a good way to cancel the request other than that I mentioned above?

robertsan
  • 1,591
  • 3
  • 14
  • 26

3 Answers3

38

You have to just change reference to your disposeBag object. Make it nil or assign new object to disposeBag.

All request will be cancelled.

Daniel Sumara
  • 2,234
  • 20
  • 25
13

Another option, besides deallocating a DisposeBag, is to use CompositeDisposable, which has an explicit dispose method.

solidcell
  • 7,639
  • 4
  • 40
  • 59
  • will this dispose the new subscription or its just like clear of RxJava? – Akhil Dad Oct 04 '17 at 04:31
  • CompositeDisposable is the ticket here. – SmileBot Aug 12 '21 at 22:19
  • Note that `CompositeDisposable` does not dispose on deinit. IT IS NOT A REPLACEMENT FOR A `DisposeBag`. You should still add your `CompositeDisposable` to another `DisposeBag` or make sure you manually dispose of it on your deinit method. – gabriellanata Jan 06 '23 at 18:55
1

You just need to reinitialise the dispose bag reference:

disposeBag = .init()

Alchi
  • 799
  • 9
  • 19