我正在 Mac 上使用 Core Data 创建一个 SQLite 文件,然后将其放入我的 iPhone 应用程序中(GitHub project 如果好奇的话)。我想使用更新的 SQLite 文件发布应用程序更新。该模型将具有相同的实体和属性,只是会有更多的数据。
我使用旧的 sqlite 文件在模拟器中运行该应用程序。导出。在 xcode 中将 sqlite 文件替换为新文件。将 NSMigratePeristentStoresAutomaticallyOption 和 NSInferMappingModelAutomaticallyOption 添加到 addPeristentStore 选项。然后再次运行应用程序,但应用程序仅显示原始文件中的相同数据。
我还尝试根据 Apple 文档和此 SO post 对模型进行版本控制并重命名 sqlite 文件。但仍然没有运气(我猜是因为模型仍然相同?)。
关于如何让应用程序读取较新的 sqlite 文件有什么想法吗?
最佳答案
我的 Core Data iPhone 应用程序使用创建项目并选择“使用 Core Data 进行存储”时提供的标准代码。
当我更改文件名常量“DB_NAME”的值并重建时,它将切换使用的 SQLite 数据库:
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent:DB_NAME]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
关于iphone - 替换应用程序中的核心数据 SQLite 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1535321/