On a recent iOS project, we had an issue with relationships between managed objects disappearing at the moment the document saved itself. The problem, it turned out, was that we were holding onto an object after immediately after creating it, and then later on setting up relationships between it and some other new objects. The ID of the object changed at the moment the document saved, and so the relationship no longer held. I found the crux of the issue by way of an excellent suggestion from Steve Tibbett's blog, which gives some great tips on troubleshooting UIManagedDocument's autosaving. The solution we came up with was to call:
+ (BOOL)obtainPermanentIDsForObjects:(NSArray *)objects error:(NSError **)error]
on the managed object context after creating each object, to ensure that its ID didn't change later (when saved). I first solved this with a subclass of NSEntityDescription with the following overload:
I have, however, just revisited this issue, and I think that a category is a little tidier and safer. Rather than having to remember to always us theNSEntityDescription subclass, you can add the category to your pre-compiled header, and then you always have the new methods available (and the old one, in case there are times you don't want the extra complexity of the permanent ID version).