Reputation: 12132
I have a fairly straightforward data model for a Core Data app. Basically, I'm bringing up a list of contacts already on the phone. When a user taps a contact, they're taken to a detail view screen, where they can select a phone number or email address. When they select any detail item, I'm storing it via Core Data.
I have one entity called Contact
, and another called Contact_Detail
.
Contact <----->> Contact_Detail
The Contact relationship to Contact_Detail has a delete rule of Deny. The Contact_Detail relationship to Contact has a delete rule of No Action.
If a user wants to remove, say, a previously selected detail item, I first fetch the
Contact_Detail objects that have the same ID as the Contact. If there is only one Contact_Detail
object returned, I then know I need to remove the contact as well from the Contact entity. If there
were more than one Contact_Detail
object returned, I just remove that particular object.
It's all working fine, and the database is getting updated as expected (detail objects removed, and when the last detail object for a particular contact is removed, so is the contact) as long as I just go back and forth between the peoplePicker and the peopleDetailViewController.
The problem I'm having is that when I select an email address, leave the detailVC, leave the peoplePicker, then
return back to the peoplePicker, and back to the same contact, and deselect the email address I previously
selected, Core Data is throwing an error that looks as though it can't delete my Contact
because there are
still relationship objects (the Contact_Detail
):
NSValidationErrorKey=details, NSLocalizedDescription=The operation couldn’t be completed. (Cocoa error 1600.), NSValidationErrorValue=Relationship objects for {( (entity: Contact_Detail; id: 0xd21afe0 ; data: ) )} on 0x5d5a780}
I can verify that the Contact_Detail
object does in fact get removed. Why can't the Contact be removed? And why
does this only happen with a particular sequence of navigation?
Let me know if I should post some code. I think this problem is causing a crash I'm seeing after continuing to work with Core Data after this first error is thrown.
Thanks!
Upvotes: 0
Views: 1669
Reputation: 21219
Why don't you set delete rules in this way?
Contact<--(cascade)-->>ContactDetail
//when Contact is removed, all details are removed too)
ContactDetail<<--(nullify)-->Contact
//when Contact Detail is removed, this particular detail is removed from Contact too
Upvotes: 4