【发布时间】:2013-04-17 06:21:19
【问题描述】:
我有一个模型对象,一个直接的 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 时上下文是否非零。
【问题讨论】:
标签: objective-c cocoa automatic-ref-counting key-value-observing