【问题标题】:When does Core Data save the managed object context by itself?Core Data 什么时候自己保存托管对象上下文?
【发布时间】:2014-08-20 16:57:27
【问题描述】:

我只有一个 OS X 10.9,非文档应用程序。
当我没有在我的托管对象上下文中显式调用 -save: 时,Core Data 什么时候自己调用 -save:
到现在,我才发现,它是在退出应用程序之前保存的。

【问题讨论】:

    标签: objective-c macos cocoa core-data nsmanagedobjectcontext


    【解决方案1】:

    如果您在创建新 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: 方法也被调用。

    【讨论】:

      【解决方案2】:

      嗯...在 iOS 中,答案很简单。它从来没有。由于 NSManagedObjectContext 的线程依赖性,我很想说这对于 OSX 是一样的。

      NSManagedObjectContext 保存自己是没有意义的。

      如果用户在创建对象的过程中退出了您的应用程序并且还没有完全完成创建对象,会发生什么情况?它会在半完成状态下保存吗?

      【讨论】:

        【解决方案3】:

        我认为 moc 根本不会自己保存

        【讨论】:

        • "mom" 是托管对象模型,通常是固定的。您可能是指“moc”== 托管对象上下文?
        • 当我没有保存就退出我的Core Data应用程序然后重新启动时,所有的数据都在这里,所以它一定已经保存了?我还在一些 NSManagedObject 子类中覆盖了 -didSave 并在它被调用时记录下来,并且每当退出应用程序时都会调用这个 get。
        • @MartinW:也许save: 在应用程序委托方法之一中被显式调用,例如在 applicationWillTerminate 中?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-03-16
        • 2012-10-23
        • 1970-01-01
        • 1970-01-01
        • 2018-09-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多