Should I check if `[managedObjectContext hasChanges]` before saving?
// Save changes if any.
NSError *error;
if ([managedObjectContext hasChanges] && ![managedObjectContext开发者_C百科 save:&error]) {
// TODO: handle this error better.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
That depends. If you're sure that changes have been made to the managedObjectContext
, then there's no need to check. However, if there's any possibility that changes have not been made, then you should check before saving.
Check out the application delegate of the CoreDataBooks Xcode sample app for examples of both these scenarios.
精彩评论