objective-c - Core Data什么时候自己保存托管对象上下文?

标签 objective-c macos cocoa core-data nsmanagedobjectcontext

我有一个仅限 OS X 10.9 的非文档应用程序。
当我没有在托管对象上下文上显式调用 -save: 时,Core Data 何时自行调用 -save:
到目前为止,我只发现它在退出应用程序之前保存。

最佳答案

如果您在创建新的 Xcode 项目时检查了“核心数据”,则应该在您的应用程序委托(delegate)中找到它:

- (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: 时才会保存上下文。但是使用 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: 方法。

关于objective-c - Core Data什么时候自己保存托管对象上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25410413/

相关文章:

java - 尝试安装 Android SDK 时出现 "java.lang.NullPointerException"

objective-c - 崩溃报告未发送给 Apple

macos - org.apache.httpd : Already loaded

objective-c - NSFileManager 在 cocoa 中创建类似 .app 的文件/文件夹

objective-c - NSView 子类 - drawRect : not called

cocoa - NSArrayController 重新排列对象错误

objective-c - 缓存 NSView 上下文

ios - Game Over Class 上的 iAd 横幅实现

objective-c - 如何在不崩溃的情况下在 Objective C 中包含 WebView

ios - NSOperationQueue 不限制并发操作