【问题标题】:Is -dealloc too late for unregistering from KVO-dealloc 从 KVO 注销是否为时已晚
【发布时间】: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


    【解决方案1】:

    想通了。模型对象被上下文为 KVO 注册了两次,但只注销了一次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 2012-09-29
      • 1970-01-01
      • 2015-02-24
      相关资源
      最近更新 更多