Coredata 删除规则混淆,xcode

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7150943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:47:08  来源:igfitidea点击:

Coredata delete rule confusion, xcode

iphoneobjective-ciosxcodecore-data

提问by James Dunay

I have a Core Data relationship between two entities, which is like this:

我有两个实体之间的核心数据关系,如下所示:

Entity A                        Entity B
aRelationship <-------------->> bRelationship

With the delete rule set to cascade.

删除规则设置为级联。

Maybe I have this wrong, but I thought that if the delete rule for both of these relationships was set to "Cascade", then when did the following...

也许我有这个错误,但我认为如果这两个关系的删除规则都设置为“级联”,那么以下是什么时候...

[context deleteObject:EntityA];

...it would also delete all the of the Entity B's associated with it. However when I log all of my entity B's it would seem that I am mistaken.

...它还会删除所有与其关联的实体 B。但是,当我记录所有实体 B 时,似乎我错了。

Could someone please shed some light on my confusion?

有人可以解释一下我的困惑吗?

Thank you very much.

非常感谢。

回答by TechZen

While it's not immediately apparent in the graphical data model editor each recipocal relationship i.e. each

虽然在图形数据模型编辑器中并不是很明显,但每个相互关系,即每个

<--> 

...is really two separate relationship each with its own delete rule. Delete rules are activate when an object of the entity with the delete rule is deleted.

...实际上是两个独立的关系,每个关系都有自己的删除规则。当具有删除规则的实体的对象被删除时,删除规则被激活。

So, if in the data model editor you have two entities Alphaand Betawith a relationship:

因此,如果在数据模型编辑器中您有两个实体AlphaBeta具有关系:

Alpha.betas<-->>Beta.alpha

… then you really have two relationships like so:

……那么你真的有两种关系:

Alpha.betas--(delete rule)-->>Beta.alpha
Beta.alpha--(delete rule)-->Alpha.betas

You neverwant to set up a delete rule like this:

永远不想设置这样的删除规则:

Alpha.betas--(cascade)-->>Beta.alpha
Beta.alpha--(cascade)-->Alpha.betas

… because deleting any one Betainstance will delete the associate Alphaobject which will trigger the deletion of all related Betaobjects. Depending on the details of your data model, a reciprocal cascade can delete a big chunk of you data by accident.

...因为删除任何一个Beta实例都会删除关联Alpha对象,这将触发所有相关Beta对象的删除。根据数据模型的详细信息,互惠级联可能会意外删除大量数据。

What you really want is:

你真正想要的是:

Alpha.betas--(cascade)-->>Beta.alpha
Beta.alpha--(nullify)-->Alpha.betas

Now, when you delete the Alphaobject, it will delete all associated Betaobjects.

现在,当您删除Alpha对象时,它将删除所有关联的Beta对象。

When a cascade is blocked, it is usually a problem with a required relationship. Can't tell for certain without details of the data model.

当级联被阻塞时,通常是所需关系的问题。如果没有数据模型的详细信息,就无法确定。

回答by xuzhe

It depends on what delete rules are you using.

这取决于您使用的是什么删除规则。

Here is what Apple said in their document:

这是Apple在他们的文件中所说的:

"When you delete a managed object it is important to consider its relationships and in particular the delete rules specified for the relationships. If all of a managed object's relationship delete rules are Nullify, then for that object at least there is no additional work to do (you may have to consider other objects that were at the destination of the relationship—if the inverse relationship was either mandatory or had a lower limit on cardinality, then the destination object or objects might be in an invalid state). If a relationship delete rule is Cascade, then deleting one object may result in the deletion of others. If a rule is Deny, then before you delete an object you must remove the destination object or objects from the relationship, otherwise you will get a validation error when you save. If a delete rule is No Action, then you must ensure that you take whatever steps are necessary to ensure the integrity of the object graph."

“当你删除一个被管对象时,重要的是要考虑它的关系,特别是为这些关系指定的删除规则。如果一个被管对象的所有关系删除规则都是 Nullify,那么对于那个对象至少没有额外的工作要做(您可能必须考虑关系目标处的其他对象——如果反向关系是强制性的或基数的下限,那么目标对象可能处于无效状态)。如果关系删除规则是级联,那么删除一个对象可能会导致删除其他对象。如果规则是拒绝,那么在删除对象之前必须从关系中删除目标对象,否则保存时会出现验证错误.如果删除规则是 No Action,那么您必须确保采取任何必要的步骤来确保对象图的完整性。”

The link of “Relationship Delete Rules.”: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdRelationships.html#//apple_ref/doc/uid/TP40001857-SW1

“关系删除规则”链接:http: //developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdRelationships.html#//apple_ref/doc/uid/TP40001857-SW1