【发布时间】:2014-08-20 16:57:27
【问题描述】:
我只有一个 OS X 10.9,非文档应用程序。
当我没有在我的托管对象上下文中显式调用 -save: 时,Core Data 什么时候自己调用 -save:?
到现在,我才发现,它是在退出应用程序之前保存的。
【问题讨论】:
标签: objective-c macos cocoa core-data nsmanagedobjectcontext
我只有一个 OS X 10.9,非文档应用程序。
当我没有在我的托管对象上下文中显式调用 -save: 时,Core Data 什么时候自己调用 -save:?
到现在,我才发现,它是在退出应用程序之前保存的。
【问题讨论】:
标签: objective-c macos cocoa core-data nsmanagedobjectcontext
如果您在创建新 Xcode 项目时检查了“核心数据”,则应该在您的应用委托中找到:
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application's managed object context before the application terminates.
[self saveContext];
}
#pragma mark - Core Data Saving support
- (void)saveContext {
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
NSError *error = nil;
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
所以当终止应用程序时,它会显式调用 save。
正确的答案是:只有当你调用 save: 时,上下文才会被保存。但是使用 xcode 模板,这是为您设置的。
上面的代码是 iOS 的,Mac OS X 是这样的
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
// Save changes in the application's managed object context before the application terminates.
if (!_managedObjectContext) {
return NSTerminateNow;
}
if (![[self managedObjectContext] commitEditing]) {
NSLog(@"%@:%@ unable to commit editing to terminate", [self class], NSStringFromSelector(_cmd));
return NSTerminateCancel;
}
if (![[self managedObjectContext] hasChanges]) {
return NSTerminateNow;
}
NSError *error = nil;
if (![[self managedObjectContext] save:&error]) {
// Customize this code block to include application-specific recovery steps.
BOOL result = [sender presentError:error];
if (result) {
return NSTerminateCancel;
}
NSString *question = NSLocalizedString(@"Could not save changes while quitting. Quit anyway?", @"Quit without saves error question message");
NSString *info = NSLocalizedString(@"Quitting now will lose any changes you have made since the last successful save", @"Quit without saves error question info");
NSString *quitButton = NSLocalizedString(@"Quit anyway", @"Quit anyway button title");
NSString *cancelButton = NSLocalizedString(@"Cancel", @"Cancel button title");
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:question];
[alert setInformativeText:info];
[alert addButtonWithTitle:quitButton];
[alert addButtonWithTitle:cancelButton];
NSInteger answer = [alert runModal];
if (answer == NSAlertFirstButtonReturn) {
return NSTerminateCancel;
}
}
return NSTerminateNow;
}
save: 方法也被调用。
【讨论】:
嗯...在 iOS 中,答案很简单。它从来没有。由于 NSManagedObjectContext 的线程依赖性,我很想说这对于 OSX 是一样的。
NSManagedObjectContext 保存自己是没有意义的。
如果用户在创建对象的过程中退出了您的应用程序并且还没有完全完成创建对象,会发生什么情况?它会在半完成状态下保存吗?
【讨论】:
我认为 moc 根本不会自己保存
【讨论】:
save: 在应用程序委托方法之一中被显式调用,例如在 applicationWillTerminate 中?