核心数据关系的自定义设置器以强制反向、互惠关系
我想为 NSManagedObject 核心数据一对多关系(Object.lated)创建一个自定义setter,这样当对象 A 添加到 B.lated 时,B 会自动添加到 A.lated,并且如果 A 被删除从 B.相关中,B 会自动从 A.相关 中删除。我之前已经能够为基本属性(例如 NSString 和 NSNumber 属性)创建自定义设置器,但这让我有些头痛。当我的函数 -(void)setRelated:(NSSet*)newSet
被调用时,似乎 Self.lated 属性已经使用 newSet 列表设置了!这里的最佳实践是什么?我还研究过使用 didChangeValueForKey:(NSString *)inKey withSetMutation:(NSKeyValueSetMutationKind)inMutationKind usingObjects:(NSSet *)inObjects
来观察此属性,但未能使其正常工作。
I want to create a custom setter for an NSManagedObject core data one-to-many relationship (Object.related), so that when object A is added to B.related, B is automatically added to A.related, and if A is removed from B.related, B is automatically removed from A.related. I have been able to create custom setters for basic properties (such as NSString and NSNumber properties) before, but this is causing me some headaches. When my function -(void)setRelated:(NSSet*)newSet
is called, it seems that the Self.related property is already set with the newSet list! What is the best practice here? I have also looked into watching this property with didChangeValueForKey:(NSString *)inKey withSetMutation:(NSKeyValueSetMutationKind)inMutationKind usingObjects:(NSSet *)inObjects
but have not been able to get it to work properly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关系的逆关系由 CoreData 管理(如果您在模型中设置了它)。如果你尝试手动完成,你最终会遇到各种各样的麻烦。
倒数是所有关系的属性,如果未在建模工具中设置它们,您应该会收到警告。
The inverse of a relationship is managed by CoreData (if you set it up in your model). If you try to do it manually you will end up with all kinds of trouble.
The inverse is a property on all relationships, you should be getting warnings if they are not set in the modeling tool.