Say I have a Core Data NSManagedObject that has an attribute which stores a image. If I want to delete the image but not the NSMAna开发者_开发问答gedObject how do I do it?
Right now I'm using. This seems to be working but I am getting intermittent crashes related to this code so I'd like to be sure.
-(void)deletePhoto{
note.thumbnail = nil; //This is a thumbnail image
[context deleteObject:note.image]; //This is an image related to the object via a to-one relationship
NSError *error;
if (![context save:&error])
NSLog(@"Error saving: %@", [error localizedDescription]);
}
I think you should let Core Data handle that. You should set the relationship to "Cascade Delete" and then set it to nil in code.
note.image = nil; // Rather than delete object.
精彩评论