Robin
Robin

Reputation: 10011

Core Data `deleteObject:` problem!

I have the following entities in core data as shown in the figure below. Core Data Model

The delete rule for all the relationship is cascade.

Questions: When ever i delete the any one of the entity object at level 2 and do [context save:&error]; all other objects in Table Entity1 gets the data fault and the app crashes due to not able to read the Entity1 object any more, giving EXC_BAD_ACCESS error.

I am using this code

for(Entity1 *entity in listOfEntitys)
{
    if(entity.Relation1)
        [context deleteObject:entity.Relation1];

    if(entity.Relation2)
        [context deleteObject:entity.Relation2];

    if(entity.Relation3)
        [context deleteObject:entity.Relation3];
}
[context save:&error];

Its not like I have never deleted any object from core data but this is the only place where problem is occurring. Can anyone Help.

Thanks.
PS. I have seen other questions on SO but none has the same kind of situation as i do.

Edit:

My problem is that when I try to delete any one object from level 2 then all the objects in the entity1 are getting data fault. some thing like this.

Printing description of listOfEntitys: 
(
    "<Entity1: 0x4dc3d80> (entity: Entity1; id: 0x4dc2d60 <x-coredata://DF11191D-0BE9-4A63-955D-0A43153290A4/Entity1/p5> ; data: <fault>)",
    "<Entity1: 0x5b06ea0> (entity: Entity1; id: 0x5b077d0 <x-coredata://DF11191D-0BE9-4A63-955D-0A43153290A4/Entity1/p6> ; data: <fault>)",
    "<Entity1: 0x4dc2cf0> (entity: Entity1; id: 0x4dc2df0 <x-coredata://DF11191D-0BE9-4A63-955D-0A43153290A4/Entity1/p7> ; data: <fault>)",
    "<Entity1: 0x4dc2b80> (entity: Entity1; id: 0x4dc3640 <x-coredata://DF11191D-0BE9-4A63-955D-0A43153290A4/Entity1/p8> ; data: <fault>)" 
)

Upvotes: 1

Views: 2005

Answers (2)

Nick Weaver
Nick Weaver

Reputation: 47241

Don't set the inverses to cascade, cause it's like a lit fuse burning down in every direction. Set them to nullify.

Upvotes: 3

Caleb
Caleb

Reputation: 124997

From the Core Data Programming Guide

If a relationship delete rule is Cascade, then deleting one object may result in the deletion of others.

If you don't want related objects to be deleted when you delete a given object, then the deletion rule for those relationships should be something other than 'cascade'. You probably want 'nullify' instead.

Upvotes: 6

Related Questions