I present unique constraints for an existing application in the new version of the model.
The model has a Person object that has an optional one-to-one relationship with the Pet object. The person deletion rule is cascaded, and the Pet feedback deletion rule is canceled. There is another entity, Job, which also has one relationship. But this deletion rule and the reverse deletion rule are not valid.
A person has a personID property, which I made unique.
In the context of the main queue, I have an instance of Person, with ID xxx, which does not have a Pet set. Let me call this man Charlie. I also create a Job and set it as "iOS Dev" and assign it a Charlie job property.
Then, in the context of the child private queue, I insert a new "Charlie" also with the identifier xxx (unique constraint) and create a Pet and set it. I do not ask a job. I maintain a detailed context without problems.
Immediately after this, I try to maintain the context of the main queue. But before that, I check the insertObjects property of the Main Queue context and see, as I expect, an instance of Person with ID xxx. I expect this second instance to be handled by unique constraints and to be “merged” with the original Person instance from the previous paragraph. But, when I actually call the MOC save method: it throws an exception, and I check the NSError object:
Domain Error = NSCocoaErrorDomain Code = 1550 "Operation could not be completed. (Cocoa error 1550.)" UserInfo = {Hanging link to an invalid object. = null
Any ideas on how to avoid the exception? I'm used to the “cheated links” in Core Data, which means the relationship is incorrect, but I looked at it many times and everything looks fine, so I'm not sure what else needs to be fixed. I have all the contexts associated with the NSMergeByPropertyObjectTrumpMergePolicy merge policy.
Obviously, this is a bit complicated, so I threw a sample project on Github in case anyone is interested: https://github.com/bpapa/core-data-unique-bug