我一直在用 coredata 解决这个问题,这让我抓狂,因为它应该是直截了当的
我目前正在开发这个应用程序的第一个版本,很明显我一直在这里和那里调整核心数据模型,
然而,每次更改核心数据模型时,我都需要卸载应用程序并重新安装新版本。
这对我来说是可以接受的,但是一旦发布,我需要能够更改更新应用程序而无需我的用户重新安装。
我错过了什么,
我是否需要编写一些代码来告诉核心数据如何将现有持久数据修改为新数据?
谢谢你的帮助
杰森
最佳答案
核心数据模型 - 迁移 - 向当前数据模型添加新属性/字段 - 无需重置模拟器或应用程序
步骤:
从编辑器创建模型版本 - 给它任何有意义的名称,例如 ModelVersion2
转到该模型版本并对您的模型进行更改。
现在转到 YourProjectModel.xcdatamodeld 并将当前版本设置为新创建的版本。
将以下代码添加到您创建持久协调器的位置 -
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
并将选项值设置为方法的选项 -
[__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]
在我的例子中,它看起来像这样:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil) {
return__persistentStoreCoordinator;
}
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"LGDataModel.sqlite"];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (! [__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return__persistentStoreCoordinator;
}
关于ios - 更改 CoreData 模型 : retro compatibility,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10374027/