RxDataSources icon indicating copy to clipboard operation
RxDataSources copied to clipboard

How to properly Dispose Subscriptions in a Table View Cell.

Open vander2675 opened this issue 5 years ago • 1 comments

Hello fellow RxSwift Developers,

Currently I'm facing a Problem with disposing Subscriptions from a UITableViewCell.

Lets say I have a UITableViewCell:


final class ACell: UITableViewCell() {

    private lazy var bag = DisposeBag()

    private var vm: ACellViewModel!

    //Skipped Init

   func bind() {
        self.vm.text
            .asDriverFilter()
            .drive(self.titleLabel.rx.text)
            .disposed(by: self.bag)
   }
   
    func set(vm: ACellViewModel) {
        self.vm = vm
        self.bind()
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        self.bag = DisposeBag()
    }
}

And assume that the text Observable in the ViewModel is producing an endless Stream of Elements, so it needs to get Disposed.

When I scroll in the UITableView everything works as expected.

But when the UIViewController wich is presenting the UITableView is dismissed, the prepareForReuse() is not called. So I can't dispose the Resources.

From what I found around the Internet, the above pattern should be right and prepareForReuse should get called.

My current workaround is to manually call prepareForReuse() on all visible Cells in the UITableView, which works.

Is there anything I don't see here?

Thank you in Advance

vander2675 avatar Jul 15 '19 10:07 vander2675

When ViewController dismiss and released, tableview will be released, and all cells and Disposebag in Cells will be released too. So no need to care about that.

trongnhan68 avatar Apr 05 '20 13:04 trongnhan68