Iphone Core Data crashing on Save

天大地大妈咪最大 提交于 2019-11-27 21:22:28

You are only guaranteed to retain access to a managed object from the context as long as a change is pending to that object (insert, update, delete). As soon as you make a call to save:, you can lose your reference to the managed object.

While you stopped getting the error when you set setRetainsRegisteredObjects:YES, you may have introduced a memory management issue, as you are setting the managed object's lifetime to be dependent on the lifetime of the managed object context. If you're passing your context around throughout your app, this could get rather large if you have a large object hierarchy.

You can read more in the Apple docs here: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdMemory.html.

kiyoshi

Solved the problem though I'm not sure I'm addressing the actual root cause. The error was eliminated when I added this line:

[managedObjectContext setRetainsRegisteredObjects:YES];

To where I create the managedObjectContext. So I guess it had to do with retain counts. I'm guessing that maybe instance variables get released partially or temporarily or something when modal views are presented? I don't know. In any case, this error was eliminated the the program works fine now.

Just to help others having the same issue, and reinforcing Steff's response above, the likely cause of this error is that you are trying to release a NSManagedObject.

I have seen many funny behaviours with CoreData on iPhone that were solved by resetting content and settings in the iPhone simulator.

I know this is an oldie but I had the same issue so thought I'd add my tuppence as to how I solved the issue, mine was caused by manually releasing the managed object within the modal view, I removed the release calls and everything is working fine :) according to the docs you shouldn't manually try and release managed objects anyway as the context will look after all of that. That's my experience anyway, search through your code for over-released values.

Greg

You must set the FRC cache to nil

I have solved a similar problem by making sure that the object is fetched, so for the example above:

if ( message == nil ) {
    message = [NSEntityDescription insertNewObjectForEntityForName:@"MailMessage" inManagedObjectContext:self.managedObjectContext];
} else {
    NSError *error = nil;
    message = (MailMessage *)[managedObjectContext existingObjectWithID:[message objectID] error:&error];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!