1

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.

hoang Cap
  • 704
  • 6
  • 18

1 Answers1

2

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

m.eldehairy
  • 695
  • 8
  • 10
  • This sounds reasonable, can you kindly send me the official docs regarding your answer? – hoang Cap Jun 01 '19 at 16:26
  • https://github.com/ReactiveX/RxSwift/blob/master/Documentation/GettingStarted.md#disposing, "When a DisposeBag is deallocated, it will call dispose on each of the added disposables." – m.eldehairy Jun 06 '19 at 10:29