核心数据:如何删除关系中的实体
我的 iPhone 上有一个复杂的核心数据图。用户实体具有许多其他实体,这些实体彼此相关且具有多种关系等...
我的问题是,当我删除用户实体时,如何删除所有相关实体。
提前致谢!
I have a complex Core Data graph on the iPhone. A user entity has a lot of other entities which are related one to the other with multiple relationships etc...
My question is how can I delete all the related entities when I delete the user entity.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的用户实体中,将每个关系删除规则设置为“级联”。
另请参阅此答案< /a> 到 iPhone 核心数据:级联删除跨多对一关系。
In your User entity, set each relationship delete rule to "cascade".
Also, see this answer to iPhone Core Data: Cascading delete across a many-to-one relationship.
您将删除规则设置为用户实体侧进行级联。当删除用户实体时,与删除规则级联有关系的所有其他实体也将被删除。如果它们同样与其他实体有关系,您也可以为这些关系设置删除规则以进行级联。当您删除最顶层的节点时,这将删除整个逻辑树。
例如,
删除用户会触发删除phoneNumber,但删除phoneNumber 只会使与用户的关系无效。用户实体不受影响。
如果你有一棵树...
user--(phoneNumbers,Cascade)->>phoneNumber--(phoneNumbers,Cascade)->>areaCodes
...然后删除用户会删除其所有phoneNumbers并删除phoneNumber会导致删除所有区号
You set the delete rule on the user entity side to cascade. When the user entity is deleted, all the other entities held in relationships with the delete rule cascade will be deleted as well. If they likewise have relationships with other entities you can set the delete rule for those relationships to cascade as well. This will delete and entire logical tree when when you delete the top most node.
E.g
Deleting user triggers the deletion of phoneNumber but deleting phoneNumber merely nullifies the relationship with user. The user entity is otherwise unaffected.
If you have a tree...
user--(phoneNumbers,Cascade)->>phoneNumber--(phoneNumbers,Cascade)->>areaCodes
...then deleting user deletes all its phoneNumbers and deleting a phoneNumber causes the deletion of all its areaCodes