gpt4 book ai didi

ios - CoreData关系困惑

转载 作者:行者123 更新时间:2023-11-29 00:42:50 24 4
gpt4 key购买 nike

假设我有两个名为 Book 和 Publisher 的实体1. 书有属性:- 作者、书名。2. Publisher 有属性:- name, type我设置了一个关系:- 出版商到书(一对多并将其设置为反向关系)

选择关系后,我可以在 DataModal 检查器中看到一行名为 Delete Rule 的三个选项 1. Nullify 2. Cascade 3. Deny

这些是什么,我只想在删除最后一本书时删除 Publisher 实体

提前谢谢你。我只是个初学者:)

最佳答案

文档很好地解释了这些删除规则

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CoreData/HowManagedObjectsarerelated.html

Relationship Delete Rules

A relationship's delete rule specifies what should happen if an attempt is made to delete the source object. Note the phrasing if an attempt is made. If a relationship's delete rule is set to Deny, it is possible that the source object will not be deleted. Consider again a department's employees relationship, and the effect of the different delete rules.

Deny If there is at least one object at the relationship destination (employees), do not delete the source object (department).

For example, if you want to remove a department, you must ensure that all the employees in that department are first transferred elsewhere (or fired!); otherwise, the department cannot be deleted.

Nullify Remove the relationship between the objects but do not delete either object.

This only makes sense if the department relationship for an employee is optional, or if you ensure that you set a new department for each of the employees before the next save operation.

Cascade Delete the objects at the destination of the relationship when you delete the source.

For example, if you delete a department, fire all the employees in that department at the same time.

No Action Do nothing to the object at the destination of the relationship.

For example, if you delete a department, leave all the employees as they are, even if they still believe they belong to that department.

It should be clear that the first three of these rules are useful in different circumstances. For any given relationship, it is up to you to choose which is most appropriate, depending on the business logic. It is less obvious why the No Action rule might be of use, because if you use it, it is possible to leave the object graph in an inconsistent state (employees having a relationship to a deleted department).

If you use the No Action rule, it is up to you to ensure that the consistency of the object graph is maintained. You are responsible for setting any inverse relationship to a meaningful value. This may be of benefit in a situation where you have a to-many relationship and there may be a large number of objects at the destination.

关于ios - CoreData关系困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39064007/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com