Instance was deallocated while key value observers

2019-03-24 13:03发布

问题:

I've got a UITableView.

Here I got different cell's. Each cell has a model. With KVO and NotificationCenter the cell listen to the model for changes. When I leave the ViewController I get this error:

An instance 0x109564200 of class Model was deallocated while key value observers were still registered with it. 
Observation info was leaked, and may even become mistakenly attached to some other object. 
Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:
<NSKeyValueObservationInfo 0x109429cc0> (
<NSKeyValueObservance 0x109429c50: Observer: 0x10942d1c0, Key path: name, Options: <New: NO, Old: NO, Prior: NO> Context: 0x0, Property: 0x10968fa00>
)

In the cell I do this when the model property is set/changed:

[_model addObserver:self
         forKeyPath:@"name"
            options:0
            context:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(modelIsInvalid:)
                                             name:@"modelIsInvalid"
                                           object:_model];

Then in the cell's dealloc:

- (void)dealloc
{
    NSLog(@"DEALLOC CELL");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_model removeObserver:self forKeyPath:@"name"];
}

In the model I also check when it get's deallocated:

- (void)dealloc
{
    NSLog(@"DEALLOC MODEL");
}

All cell's are deallocated before all the models, but still I get this error. Also I'm not sure how to set the breakpoint mentioned in the error.

回答1:

It won't work because the cells are being reused. So when the cell goes off the screen it's not deallocated, it goes to reuse pool.

You shouldn't register notifications and KVO in cell. You should do it in table view controller instead and when the model changes you should update model and reload visible cells.



回答2:

I found the answer. I can't delete the thread, someone has answered :) Maybe it will be useful for someone.

The problem is that the UITableView will dequeue the same cell used before, for a row longer down (that becomes visible when scrolling far enough).

In the setter I now have:

// Before we set new model
if (_model) {
    [_model removeObserver:self forKeyPath:@"name"];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"modelIsInvalid" object:_model];
}

_model = model;

[_model addObserver:self
         forKeyPath:@"name"
            options:0
            context:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(modelIsInvalid:)
                                             name:@"modelIsInvalid"
                                           object:_model];


回答3:

Based on the accepted answer (which is correct) you can solve it by removing the observer on the "prepareForReuse" method of the cell.

That method will be called before reusing the cell on scrolling etc.. thus you wont have any problem.

- (void)prepareForReuse{
    [_model removeObserver:self forKeyPath:@"name"];
}


回答4:

There may be a possibility that your view Controller is not calling dealloc method because it's reference might be hold by someone and your dealloc method is not getting called. You can remove observer at your viewDidUnload: or viewWillDisappear: method or you can trace your controller into instrument for any retain



回答5:

The best place to do this for Cells and Reusable views is in willMove(toSuperiew)

override func willMove(toSuperview newSuperview: UIView?) { if newSuperview == nil { // check for nil means this will be removed from superview self.collectionView?.removeObserver(self, forKeyPath: "contentSize") } }