Cocoa 键值编码和逆关系属性

发布于 2024-09-10 21:06:26 字数 757 浏览 0 评论 0原文

我试图弄清楚 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱ヾ落雨殇 2024-09-17 21:06:26

键值编码中没有任何东西可以直接帮助您维持逆关系。不过,您可以利用键值观察来做到这一点。

您想要做的主要事情是:

  • 实现您自己的与 NSManagedObject 等效的类,而不是直接为您的实体类子类化 NSObject,因为毕竟您不能保证任意 NSObject 子类将与您提出的系统一起工作和。 (或者,您可以将他们需要的所有内容编入协议中。EOF 使用 EOEnterpriseObject 协议来完成此操作。)
  • 实现您自己的相当于 NSManagedObjectContext 的类来处理对实体类实例的观察,因此它可以告诉它们在适当的时间。注意,KVO触发的逆维护不需要重新触发KVO,否则会触发逆维护等等。
  • 实现您自己的相当于 NSManagedObjectModel 的类层次结构,以便相当于 NSManagedObjectContext 知道实体类实例的哪些属性可以具有反向关系,以及这些属性是什么。

考虑到所有这些,您可能只想使用 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:

  • Implement your own class equivalent to NSManagedObject, rather than just subclassing NSObject directly for your entity classes, since after all you can't be guaranteed arbitrary NSObject subclasses will work with the system you come up with. (Alternatively, you could codify everything they need in a protocol. EOF did this with the EOEnterpriseObject protocol.)
  • Implement your own class equivalent to NSManagedObjectContext to handle the observation of instances of your entity classes, so it can tell them to do inverse maintenance at the appropriate time. Note that inverse maintenance triggered by KVO needs to not re-trigger KVO which would trigger inverse maintenance and so on.
  • Implement your own class hierarchy equivalent to NSManagedObjectModel so your equivalent to NSManagedObjectContext knows what properties of instances of your entity classes can have inverse relationships, and what those 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文