Cocoa 键值编码和逆关系属性
我试图弄清楚 KVC 机制在处理彼此相反的关系属性时是否提供任何帮助。我将使用人为的标准部门/员工示例。
@interface Department : NSObject {
NSMutableArray *employees;
}
@property (retain) NSMutableArray *employees;
// KVC methods for mutating the employees array
@end
@interface Employee : NSObject {
Department *department;
}
@property (retain) Department *department;
仅此(以及省略的KVC收集方法)KVC为管理双向关系提供了什么样的帮助?我现在只处于概念阶段。我知道使用 Core Data,我可以设置显式反向关系,因此当我使用 [myDepartment insertObject:newEmployee inEmployeesAtIndex:lastIndex];
时,newEmployee.department
会自动设置为 < code>myDepartment,但是我可以仅使用 KVC 和运行时来实现此目的,还是需要 Core Data?
预先感谢您的帮助。
编辑有点不相关但也很重要,在我的代码中,我将员工的属性作为保留
放入部门,但我想知道这是否会导致保留周期?
I'm trying to figure out if the KVC mechanisms provide any sort of help when dealing with relationship properties which are inverse relationships of each other. I'll use the contrived and standard Department/Employee example.
@interface Department : NSObject {
NSMutableArray *employees;
}
@property (retain) NSMutableArray *employees;
// KVC methods for mutating the employees array
@end
@interface Employee : NSObject {
Department *department;
}
@property (retain) Department *department;
With just this (and the omitted KVC collection methods) what kind of help does KVC provide for managing the relationship in both directions? I'm only at the conceptual stage right now. I know with Core Data I can set explicit inverse relationships and thus when I use [myDepartment insertObject:newEmployee inEmployeesAtIndex:lastIndex];
, then newEmployee.department
is automatically set to myDepartment
, but can I achieve this with solely KVC and the runtime, or do I need Core Data?
Thanks in advance for helping.
Edit Somewhat unrelated but also important, in my code I put Employee's property to Department as being retain
but I'm wondering if this will cause a retain cycle?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
键值编码中没有任何东西可以直接帮助您维持逆关系。不过,您可以利用键值观察来做到这一点。
您想要做的主要事情是:
考虑到所有这些,您可能只想使用 Core Data,因为它已经完成了所有这些工作,并且多年来经过了广泛的调整,可以做得很好。
There's nothing directly in Key-Value Coding that will help you maintain inverse relationships. You could leverage Key-Value Observing to do so however.
The main things you'll want to do are:
Given all of that, you'll probably just want to use Core Data, because it already does all of that and has been extensively tuned over the years to do it quite well.