Memory management mechanism in RxSwift

258 views Asked by At

I've been studying Rxswift for a while, so far I'm able to apply Rxswift to my projects. However I still don't understand how RxSwift manages the memory resources, especially the Disposable objects. For example:

func foo() {
            let s = Observable.of(1).subscribe(onNext: { print($0) })
            s.disposed(by: _disposeBag)
        }

In the method above, s is a Disposable object, and as I understand, the object persists until it is disposed (by either the _disposeBag being released or by calling .dispose() directly on s), meaning that something is holding a strong reference to s, but I don't know what that something is.

I've been looking for documentations on this matter, but no hope so far. Can someone please point me to the right direction?

Thanks in advance guys.

1

There are 1 answers

2
m.eldehairy On

The DisposeBag holds a strong reference to the disposable when you call disposed(by:), and assuming the DisposeBag is retained by a UIViewController for example, when the UIViewController is deallocated the DisposeBag will be deallocated and in its deinit() function it call dispose() of every disposable it retains.

HTH