views:

22

answers:

1

I have a simple Core Data model:

Entity Team (name, logo) Entity Sport (name, teams to->many Teams)

So, a Sport can have many Teams

I create the Team first, then add or edit a Sport and add the Teams. If I delete a Team, my app starts crashing. I believe because it is looking for the deleted reference.

So, how can I restrict via Core Data, not to delete an Entity which is being used as reference from another Entity's relation.

I don't want to set the relationship to nil. And I don't want to go and delete the Team from the Sport first, and then delete it alone. I just want it to notify that I can't delete it. i.e. I'm looking for a built-in checker.

A: 

You might want to read up on Core Data's "Relationship Delete Rules", if you haven't already. And you want to make sure that your Team <<---> Sport relationship is bidirectional.

Alternatively, instead of calling the generated removeXxxObject and removeXxxs methods directly, you may want to implement wrapper methods that do necessary checking before calling 'remove' methods.

westsider
Why is bidirectional important?
elcool
I found that things don't work as well with unidirectional relationships, though in one case that is what I need - so that's what I have and it's fine. I think that Core Data does a better job of maintaining model integrity when relationships are bidirectional. It may come down to simply having delete rules (even default ones) to work with during deletions. If you get a chance, check out Marcus Zarra's Core Data book; page 13 addresses this issue.
westsider