OGeek|极客世界-中国程序员成长平台

标题: ios - 核心数据在启动之间不持久 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:11
标题: ios - 核心数据在启动之间不持久

我在使用 Core Data 时遇到了一个奇怪的问题。在一个特定的 View Controller 中,我从 Core Data 中的对象加载 View 。当我运行该应用程序时,它第一次加载此 View 时,Core Data 不会从我的获取中返回任何内容。所以我重新填充核心数据,每次显示 View 时,它都会正确地从核心数据中获取对象。但是,每次启动应用程序时,它都不会在 Core Data 中找到任何内容,然后必须再次从头开始创建对象。

那么,什么会导致 Core Data 对象在应用程序运行时持续存在,但在启动之间不存在呢?我没有做任何事情来删除任何对象。

编辑:有没有办法查看 Core Data 中的实际内容?像文件或我可以查看的东西?这样会更容易调试。



Best Answer-推荐答案


确保在更改后保存上下文。 模板方法是:

- (void)saveContext {
    NSError *error = nil;
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != 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.
            NKLOG(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }
    }
}

如果你想打开你的数据库,你可以试试这个名为 SQLite Manager 的 firefox 插件。

然后搜索您的 .sqlite 文件,您的应用的默认路径是:

/Users/YOUR_USER/Library/Application Support/iPhone Simulator/IOS_VERSION/Applications/GENERATED_HASH/Documents/YOUR_APP.sqlite

关于ios - 核心数据在启动之间不持久,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16720022/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4