-dealloc从KVO取消注册为时已晚

时间:2013-04-23 21:26:22

标签: objective-c cocoa automatic-ref-counting key-value-observing

我有一个模型对象,一个直接的NSObject子类,其属性由使用KVO的上下文对象观察。

我在模型对象的dealloc中取消注册KVO,如下所示:

- (void) dealloc
{
    [self.context unregisterObject:self];
}

上下文的方法如下所示:

- (void) unregisterObject:(MyCustomObject*) inObject
{   
    for (NSString *property in [inObject propertyNamesToObserve])
    {
        [inObject removeObserver:self forKeyPath:property context:(void*)kCustomContext];
    }
}

我仍然收到来自运行时的消息,如下所示,所以我想知道-dealloc是否来不及从KVO取消注册?

An instance 0x10045fc10 of class MyCustomObject 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:

根据这篇文章,我正在做的应该没问题: KVO and ARC how to removeObserver

还是我忽略了什么?当调用dealloc时,我检查了上下文是非nil。

1 个答案:

答案 0 :(得分:2)

想出来。模型对象由上下文两次注册为KVO,但仅注册一次。