简体   繁体   English

两次发射之间的核心数据不持久

[英]core data not persisting between launches

I'm running into a strange problem with Core Data. 我在Core Data方面遇到了一个奇怪的问题。 In a particular view controller I'm loading the views from objects in Core Data. 在特定的视图控制器中,我正在从Core Data中的对象加载视图。 When I run the app, the first time it loads this view Core Data returns nothing from my fetch. 当我运行该应用程序时,它第一次加载此视图时,Core Data不会从我的提取中返回任何内容。 So I repopulate Core Data, and every time the view is displayed after that it correctly fetches the objects from Core Data. 因此,我重新填充了核心数据,并且在每次显示视图之后,它都会正确地从核心数据中获取对象。 However, each time the app is launched, it doesn't find anything in Core Data and then has to create the objects from scratch again. 但是,每次启动该应用程序时,它都不会在Core Data中找到任何内容,因此必须再次从头开始创建对象。

So what would cause Core Data objects to persist while the app is running but not between launches? 那么,什么会导致Core Data对象在应用程序运行时持久存在,而不在启动之间持久存在? I'm not doing anything to delete any objects. 我没有做任何删除任何对象的操作。

EDIT: And is there any way to view what is actually in Core Data? 编辑:有没有办法查看核心数据中的实际内容? Like a file or something I could look at? 像文件还是我可以看的东西? That would make it easier to debug this. 这样可以更轻松地调试它。

Make sure that you are saving the context after your changes. 确保更改后保存上下文。 The template method is: 模板方法是:

- (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();
        }
    }
}

If you want to open your database, you can try this firefox addon called SQLite Manager 如果要打开数据库,可以尝试使用名为SQLite Manager的 firefox插件。

And search for your .sqlite file, the default path for your app would be: 并搜索您的.sqlite文件,您的应用程序的默认路径为:

/Users/YOUR_USER/Library/Application Support/iPhone Simulator/IOS_VERSION/Applications/GENERATED_HASH/Documents/YOUR_APP.sqlite /用户/您的用户/库/应用程序支持/ iPhone模拟器/ IOS_VERSION /应用程序/ GENERATED_HASH /文档/YOUR_APP.sqlite

All the files for you application can be inspected by finding where the simulator has put your application. 可以通过找到模拟器将应用程序放置在哪里来检查应用程序的所有文件。 You can put this out with an NSLog( @"My database is at: '%@'", theDatabaseURL.path ); 您可以使用NSLog( @"My database is at: '%@'", theDatabaseURL.path );

Since everything you do in an NSManagedObjectContext is maintained in memory it makes sense that it would persist while the application is running but be gone the next time the application launches if the persistent store is not being set up properly or a save operation is not being triggered. 由于您在NSManagedObjectContext执行的所有操作均保存在内存中,因此有理由认为,如果未正确设置持久性存储或未触发保存操作,它将在应用程序运行时持续存在,但在下次启动应用程序时消失。 。

It might help if you showed the part of your code where you open, initialize, and save your data. 如果在打开,初始化和保存数据的位置显示了代码的一部分,这可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM