Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coredata delete rule confusion, xcode

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.

Could someone please shed some light on my confusion?

Thank you very much.

like image 296
James Dunay Avatar asked Aug 22 '11 17:08

James Dunay


People also ask

What is delete rule in Core Data?

A delete rule defines what happens when the record that owns the relationship is deleted. Select the notes relationship of the Category entity and open the Data Model Inspector on the right. By default, the delete rule of a relationship is set to nullify. Core Data supports four delete rules: No Action.

What is the function of a deletion rule?

Setting the Delete Rule to Protect prevents deleting records of the main Entity while there are associated records in the related Entity. This behavior is ensured by a database constraint created on the reference attribute.

What is relationship in Core Data?

Inverse relationships enable Core Data to propagate change in both directions when an instance of either the source or destination type changes. Every relationship must have an inverse. When creating relationships in the Graph editor, you add inverse relationships between entities in a single step.


2 Answers

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 Alpha and Beta with a relationship:

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 never want to set up a delete rule like this:

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

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

What you really want is:

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

Now, when you delete the Alpha object, it will delete all associated Beta objects.

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.

like image 198
TechZen Avatar answered Nov 09 '22 08:11

TechZen


It depends on what delete rules are you using.

Here is what Apple said in their document:

"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."

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

like image 3
xuzhe Avatar answered Nov 09 '22 07:11

xuzhe