Search code examples
iosobjective-creactive-cocoa

ReactiveCocoa- How to dispose of a signal using 'switchToLatest'?


I have a UITableViewCell with a viewModel property. As the tableview cell is getting reused I would like it to bind to properties of its latest viewModel, like so:

RAC(self.titleLabel, text) =
[[RACObserve(self, viewModel) map:^id(MyViewModel *viewModel) {
    return RACObserve(viewModel, title);
}]
switchToLatest];

The problem I'm seeing is that the cell is never getting released when it should be. Is there a way to dispose of the signal when the cell should be dealloc-ed?


Solution

  • My mistake! RACObserve() will retain self– I was missing a @strongify(self). Solved with:

    @weakify(self);
    RAC(self.titleLabel, text) =
    [[RACObserve(self, viewModel) map:^id(MyViewModel *viewModel) {
       @strongify(self);
       return RACObserve(viewModel, title);
    }]
    switchToLatest];