Coredata delete rule confusion, xcode

16,795

Solution 1

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.

Solution 2

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

Share:
16,795
James Dunay
Author by

James Dunay

Flash Developer / Objective C

Updated on July 02, 2022

Comments

  • James Dunay
    James Dunay almost 2 years

    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.