views:

287

answers:

2

Hi everyone,

I have a complex Core Data graph on the iPhone. A user entity has a lot of other entities which are related one to the other with multiple relationships etc...

My question is how can I delete all the related entities when I delete the user entity.

Thanks in advance!

+1  A: 

In your User entity, set each relationship delete rule to "cascade".

Also, see this answer to iPhone Core Data: Cascading delete across a many-to-one relationship.

gerry3
A: 

You set the delete rule on the user entity side to cascade. When the user entity is deleted, all the other entities held in relationships with the delete rule cascade will be deleted as well. If they likewise have relationships with other entities you can set the delete rule for those relationships to cascade as well. This will delete and entire logical tree when when you delete the top most node.

E.g

user--(phoneNumbers,Cascade)->>phoneNumber
user<--(user,nullify)--phoneNumber

Deleting user triggers the deletion of phoneNumber but deleting phoneNumber merely nullifies the relationship with user. The user entity is otherwise unaffected.

If you have a tree...

user--(phoneNumbers,Cascade)->>phoneNumber--(phoneNumbers,Cascade)->>areaCodes

...then deleting user deletes all its phoneNumbers and deleting a phoneNumber causes the deletion of all its areaCodes

TechZen
Thank you so much to gerry3 and TechZen... it is clear now!
ncohen