Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually disposing a DisposeBag in RxSwift

Tags:

swift

rx-swift

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?

like image 670
robertsan Avatar asked Aug 16 '16 07:08

robertsan


People also ask

How to dispose observable RxSwift?

Thread safe bag that disposes added disposables on deinit . This returns ARC (RAII) like resource management to RxSwift . In case contained disposables need to be disposed, just put a different dispose bag or create a new one in its place. In case explicit disposal is necessary, there is also CompositeDisposable .

What is dispose in RxSwift?

RxSwift: DisposeBag Used to avoid memory leaks as the subscription will not be disposed of correctly without. DisposeBag holds disposables. DisposeBag allows us not to have to dispose of each subscription individually.


2 Answers

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

All request will be cancelled.

like image 170
Daniel Sumara Avatar answered Oct 08 '22 06:10

Daniel Sumara


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

like image 32
solidcell Avatar answered Oct 08 '22 08:10

solidcell