Objective c 核心数据何时自行保存托管对象上下文?

Objective c 核心数据何时自行保存托管对象上下文?,objective-c,macos,cocoa,core-data,nsmanagedobjectcontext,Objective C,Macos,Cocoa,Core Data,Nsmanagedobjectcontext,我只有一个OS X 10.9,非文档应用程序。 当我没有在托管对象上下文上显式调用-save:时,核心数据何时单独调用-save: 到目前为止,我只发现,它在退出应用程序之前保存。我不认为moc是自己保存的。我不认为moc是自己保存的。嗯。。。在iOS中,答案很简单。永远不会。由于NSManagedObjectContext的线程依赖性,我很想说OSX也是如此 NSManagedObjectContext保存自己是没有意义的 如果用户在创建对象的过程中中途退出应用程序,但尚未完全完成创建,会发生

我只有一个OS X 10.9,非文档应用程序。
当我没有在托管对象上下文上显式调用
-save:
时,核心数据何时单独调用
-save:

到目前为止,我只发现,它在退出应用程序之前保存。

我不认为moc是自己保存的。

我不认为moc是自己保存的。

嗯。。。在iOS中,答案很简单。永远不会。由于NSManagedObjectContext的线程依赖性,我很想说OSX也是如此

NSManagedObjectContext保存自己是没有意义的


如果用户在创建对象的过程中中途退出应用程序,但尚未完全完成创建,会发生什么情况?它会以半完整状态保存吗?

嗯。。。在iOS中,答案很简单。永远不会。由于NSManagedObjectContext的线程依赖性,我很想说OSX也是如此

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的,对于MacOSX它看起来像

- (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;
}
这里还调用了
保存:
方法。

如果在创建新的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的,对于MacOSX它看起来像

- (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:
方法。

“mom”是托管对象模型,通常是固定的。您的意思可能是“moc”==托管对象上下文?当我退出核心数据应用程序而不保存,然后重新启动时,所有数据都在这里,所以它一定已经保存了?我也在一些NSManagedObject子类中重写了-didSave,并在调用时记录了它,每次退出应用程序时都会调用该get。@MartinW:也许在应用程序委托方法之一中显式调用了
save:
,例如在applicationWillTerminate中?“mom”是托管对象模型,通常是固定的。您的意思可能是“moc”==托管对象上下文?当我退出核心数据应用程序而不保存,然后重新启动时,所有数据都在这里,所以它一定已经保存了?我也在一些NSManagedObject子类中重写了-didSave,并在调用时记录了它,并且每次退出应用程序时都会调用此get。@MartinW:也许在应用程序委托方法之一中显式调用了
save:
,例如在applicationWillTerminate中?