安全键值关键路径的观察 [英] Safe Key Value Observing of keypaths
问题描述
我试图在关键路径上实现一个安全的键值。让我们假设我有一个名为 person
的数据模型对象,它有一个工作区
属性。 工作区
又有一个地址
属性,我希望观察所以我设置键值观察与以下调用:
I am trying to implement a safe key value observing on keypaths. Let's suppose that I have a data model object named person
that have a workplace
property. The workplace
in turn have a address
property that I wish to observe so I set up key value observing with the following call:
[person addObserver:theObserver
forKeyPath:@"workplace.address"
options:NSKeyValueObservingOptionNew
context:NULL];
这个工作正常,直到人不改变工作场所。一旦发生这种情况:
This works fine until the person does not change workplace. As soon as this happens:
person.workplace = newWorkplace;
KVC系统正确地崩溃了应用程序,说oldAddress被释放,而键值观察者仍然注册它。 (oldAddress以前工作区的地址)。
the KVC system crashes the application correctly saying that "oldAddress was deallocated while key value observers were still registered with it". (being oldAddress the address of the previous workplace).
不幸的是,我不能调整'person'对象的类的实现,以通知观察者工作场所走开。有什么模式,以避免这种崩溃吗?也许可以得到一些其他通知?
Unfortunately I can not tweak the implementation of the class of 'person' object to notify the observer that workplace is going to go away. Are there any pattern to avoid this kind of crash? Maybe one can get some other notifications? How is the keypath being traversed in the case of KVC and do you have access to this chain?
推荐答案
编辑2
在KVO花了一些时间后,我发现在你的情况下,你应该观察 person.workplace.address
而不是 workplace.address
。当你观察 person.workplace.address
时,你会实现两件事:
After spending some more time with KVO, I found that in your case, you should be observing person.workplace.address
instead of workplace.address
. When you observe person.workplace.address
, you achieve 2 things:
1) c $ c> person 对象,你可以绝对控制对象的生命周期。
1) Since you owned the person
object, you have absolute control over your object's life cycle. You can now removeObserver at a time of your own choosing.
2)当工作
对象更改时,KVO机制将自动地观察新工作区
的新地址
。当然,它会通知你新的地址
。
2) When workplace
object is changed, the KVO mechanism will "automagically" observe the new address
of the new workplace
. Of course, it will notify you of the new address
.
现在,你可以观察地址
,而不用担心随机更换工作场所
。这是KVO真正隐藏的力量之一。这允许子类安全地观察任何超类的对象,而不必知道它们的生命周期。
Now, you can observe the address
without fearing workplace
being replaced at random time. This is one of the true hidden power of KVO. This allow subclasses to safely observe any superclass's objects without knowing about their life-cycles.
编辑1
此线程中接受的答案最好地描述了您的情况。您不应该首先观察属性地址
,因为您无法控制工作区
。你有设计问题。
The accepted answer in this thread best described your situation. You should not have observe the property address
in the first place, since you have no control over the life-cycle of workplace
. You have a design issue.
原始
您可以观察keyPath
。调用此keyPath时,您只需为 workplace.address
删除Observer。
You can observe the keyPath workplace
on the person object. When this keyPath is invoked, you simply removeObserver for workplace.address
.
person addObserver:theObserver
forKeyPath:@workplace
options:[NSKeyValueObservingOptionNew]
context:NULL];
[person addObserver:theObserver forKeyPath:@"workplace" options:[NSKeyValueObservingOptionNew] context:NULL];
这篇关于安全键值关键路径的观察的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!