KVO对象属性在一对多关系内 [英] KVO object properties within to-many relationship

查看:459
本文介绍了KVO对象属性在一对多关系内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Core Data to-many关系,包含Parent< --- >> Child。我想设置一个键值观察机制,当任何子对象的属性(例如firstName,lastName)改变它触发一个通知。当使用标准的KVO语法:

I have a Core Data to-many relationship consisting of Parent <--->> Child. I would like to setup a key-value observing mechanism such that when a property (e.g. firstName, lastName) on any of the Child objects changes it triggers a notification. When using the standard KVO syntax:

[self.parentObject addObserver:self forKeyPath:@"children" options:NSKeyValueObservingOptionNew context:NULL]

这只会在关系本身被修改时通知(即,子对象被添加或从关系),而不是当这些Child对象之一的属性改变时。显然,这是它的设计操作,所以没有什么不对的发生,但我怎么能改用KVO来实现我的要求?

this only notifies when the relationship itself is modified (i.e. a Child object is added or removed from the relationship), rather than when a property of one of those Child objects changes. Obviously this is how it was designed to operate, so there's nothing wrong with that happening, but how can I instead use KVO to achieve my desired requirement?

提前感谢!

推荐答案

AFAIK没有内置的方法来观察集合对象属性与一行代码。相反,当对象从集合中插入/删除时,必须添加/删除观察器。

AFAIK there is no built-in way to observe collection object properties with a single line of code. Instead you have to add/remove observers when the object is inserted/removed from your collection.

您可以在这里找到解释和示例项目: https://web.archive.org/web/20120319115245/http://homepage.mac.com/mmalc/CocoaExamples/controllers。 html
(请参阅观察集合与观察集合中对象的属性不同)

An explanation and a sample project can be found over here: https://web.archive.org/web/20120319115245/http://homepage.mac.com/mmalc/CocoaExamples/controllers.html (See the "Observing a collection is not the same as observing the properties of the objects in a collection" section)

更新:

链接已中断 - 我将其更改为archive.org快照。

Update:
The Link broke - I changed it to an archive.org snapshot.

这篇关于KVO对象属性在一对多关系内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆