Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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)</p> <p>Solved (at least in my case). Here is what was happening:</p> <p>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 <strong>myCell.modelObject</strong>.</p> <p>As I noted, the error was happening on every search <em>except the initial one</em>. To debug, I set a symbolic breakpoint on <strong>NSKVODeallocateBreak</strong> and saw that this was happening upon <em>re-using one of my custom cells</em>: </p> <pre><code>- (void)configureWithModelObject:(ModelObject*)aModelObject { // @property (nonatomic, retain) ModelObject *modelObject; self.modelObject = aModelObject; // &lt;-- NSKVODeallocateBreak paused here .... } </code></pre> <p>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 <strong>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.</strong></p> <p><strong>Solution:</strong> 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:</p> <pre><code>- (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; // &lt;-- NSKVODeallocateBreak paused here .... </code></pre> <p><strong>Note</strong>: I still need to call [modelObject removeObserver...] in [cell dealloc] to handle the situation where the view controller itself deallocates.</p> <p>I hope this is helpful to others with a similar pattern.</p> <p>Best, Steve</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload