Manually disposing a DisposeBag in RxSwift
Asked Answered
B

3

26

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?

Bergamo answered 16/8, 2016 at 7:49 Comment(0)
A
39

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

All request will be cancelled.

Arithmetician answered 16/8, 2016 at 8:18 Comment(0)
I
13

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

Ipoh answered 18/8, 2016 at 1:15 Comment(3)
will this dispose the new subscription or its just like clear of RxJava?Hanrahan
CompositeDisposable is the ticket here.Sproul
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.Riotous
F
2

You just need to reinitialise the dispose bag reference:

disposeBag = .init()

Fiora answered 23/5, 2022 at 13:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.