Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KVO: UITableViewCell observing model property - how to unregister when model deallocs?

I have UITableViewCells that have an imageView that loads a UIImage from a model object myObject. The UIImage property on myObject is loaded asynchronously and I observe changes to it from the tableViewCell to know when I can reload it (and replace the placeholder image). Here is my problem secenario:

  1. User performs a search, which populates an array of model objects
  2. Those model objects are represented in UITableViewCells, and the iconImg property is observed for async download completion. (This works great, so far).
  3. If the user does another search, I discard those original model objects, and that results in a "An instance of class Merchant was deallocated while key value observers were still registered with it" message, which doesn't sound good.

The dealloc methods for my UITableViewCells unregister the cell, but that isn't called when I dealloc the underlying model in the scenario above. Is there a clean way to tell my cells to unregister when the observed instance deallocs? Can I just remove all observers in the model object's dealloc?

Side question: why doesn't KVO automatically remove registered observers from an object when it deallocs?

like image 217
Steve N Avatar asked Mar 16 '11 21:03

Steve N


2 Answers

UPDATE: see my comment for the best way to fix this. That said, to understand what's happening, this question is still informative...(end update)

Solved (at least in my case). Here is what was happening:

My view controller is used for displaying search results, and it stores those search results as an array of model objects. I use a custom UITableViewCell subclass to display each search result and, as part of configuring it, I store the associated model object (search result) within a property of the custom cell, call it myCell.modelObject.

As I noted, the error was happening on every search except the initial one. To debug, I set a symbolic breakpoint on NSKVODeallocateBreak and saw that this was happening upon re-using one of my custom cells:

- (void)configureWithModelObject:(ModelObject*)aModelObject {
    // @property (nonatomic, retain) ModelObject *modelObject;
    self.modelObject = aModelObject;  // <-- NSKVODeallocateBreak paused here
    ....
}

So, my view controller, upon getting a second search result set, would release the array containing the original search's model objects. Those still retained by my re-usable custom cell instances would stick around, at least until those cells were re-used again. At that point, when self.modelObject is updated, the previous modelObject is released again and finally deallocates but because the cell that was observing it was not deallocated, but instead was re-used, my [modelObject removeObserver...] call in [myCell dealloc] was not called.

Solution: When configuring my custom cell, I need to check if the model object is already set, which is an indication that this cell is being re-used. If so, I stop observing the original modelObject before updating the property:

- (void)configureWithModelObject:(ModelObject*)aModelObject {
    if(modelObject != nil) {
        // I'm being re-used! Stop observing old model object!
        [modelObject removeObserver:self forKeyPath:@"keyPathIWasObserving"];
    }
    self.modelObject = aModelObject;  // <-- NSKVODeallocateBreak paused here
    ....

Note: I still need to call [modelObject removeObserver...] in [cell dealloc] to handle the situation where the view controller itself deallocates.

I hope this is helpful to others with a similar pattern.

Best, Steve

like image 105
Steve N Avatar answered Jan 02 '23 11:01

Steve N


I suspect you doesn't retain you model object in cell because otherwise it wouldn't be deallocated before cell has a chance to unsubscribe from it. As Stephen Poletto remarked some code will help to understand your problem.

why doesn't KVO automatically remove registered observers from an object when it deallocs?

Because only object and notification center know about subscription, but

1) notification center doesn't know when object is dealloc'ed

2) object knows but imagine that every object in system is checking if it was subscribed to something or something was subscribed to it. There will be a great performance loss. (I did it in home-baked bindings but it was performed only on classes which instances were subscribed at least once, for a small set of objects)

We are on device with limited resources and we're supposed to clean up everything we did explicitly and on time.

like image 24
hoha Avatar answered Jan 02 '23 12:01

hoha