danB
danB

Reputation: 173

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?

Upvotes: 3

Views: 1183

Answers (2)

Chris Gummer
Chris Gummer

Reputation: 4782

Let me add that there's a more concise way of writing that. Assuming you're using storyboards or xibs for your cells:

- (void)awakeFromNib {
    [super awakeFromNib];
    RAC(self, textLabel.text) = RACObserve(self, viewModel.title);
}

Upvotes: 3

danB
danB

Reputation: 173

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];

Upvotes: 3

Related Questions