原帖:http://blog.163.com/djx421@126/blog/static/48855136201411381212985/
一般程序app升级时,数据库有可能发生改变,如增加表字段,增加表等。 此时有两种操作:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *folderPath = [NSString stringWithFormat:@"%@/Calendar",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject]];
if(![fileManager fileExistsAtPath:folderPath]){//如果不存在,则说明是第一次运行这个程序,那么建立这个文件夹
[fileManager createDirectoryAtPath:folderPathwithIntermediateDirectories:YES attributes:nil error:nil];
}
NSURL *storeURL = [NSURL fileURLWithPath:[folderPathstringByAppendingPathComponent:@"Calendar.sqlite"]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES],
NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES],
NSInferMappingModelAutomaticallyOption, nil];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinatoraddPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURLoptions:options error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
第二步:增加一个新版本的数据模型
选择Calendar.xcdatamodel文件 点击Editor -> Add Model Version 弹出一个对话框,填写Version Name (如 Calendar 2) 和Based on model (如 Calendar)。
第三步:继续选择Calendar.xcdatamodel文件 ,按option + command + 0 键,打开xcode最右侧栏, 在model version 的Current 中选择Calendar 2.
第四步:修改你的Calendar 2.xcdatamodel文件(如新增字段,添加表等操作),然后记得更新你改动表的entity代码。(注:这个步骤顺序一定要注意,千万不能在原Calendar.xcdatamodeld 上直接修改表结构,再添加新版本,这样的话会一直报错)
PS:NSURL *storeURL = [NSURL fileURLWithPath:[folderPath stringByAppendingPathComponent:@"Calendar.sqlite"]]; 这里还是Calendar.sqlite,而不是Calendar 2.sqlite,因为在第三步中已经选择了Calendar 2。
ok,开始build吧
如果模拟器运行报错,删掉应用,重新run.
特别注意:
在为属性或者Entity等重命名时,即更新字段名,我们需要在Xcode右侧辅助工具栏中找到 Versioning -> RenamingID,设置Reanaming Identifier为之前对应的名称。
参考:http://www.tuicool.com/articles/B3YNNj
渐进式迁移 (Progressive Migrations)
与其为每个之前的数据模型到最新的模型间都建立映射模型,还不如在每两个连续的数据模型之间创建映射模型。以前面的例子来说,版本 1 和版本 2 之间需要一个映射模型,版本 2 和版本 3 之间需要一个映射模型。这样就可以从版本 1 迁移到版本 2 再迁移到版本 3。显然,使用这种迁移的方式时,若用户在较老的版本上迁移过程就会比较慢,但它能节省开发时间并保证健壮性,因为你只需要确保从之前一个模型到新模型的迁移工作正常即可,而更前面的映射模型都已经经过了测试。
总的想法就是手动找出当前版本 v 和版本 v+1 之间的映射模型,在这两者间迁移,接着继续递归,直到持久化存储与当前的数据模型兼容。
这一过程看起来像下面这样(完整版可以在示例项目里找到):
- (BOOL)progressivelyMigrateURL:(NSURL *)sourceStoreURL
ofType:(NSString *)type
toModel:(NSManagedObjectModel *)finalModel
error:(NSError **)error
{
NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:type
URL:sourceStoreURL
error:error];
if (!sourceMetadata) {
return NO;
}
if ([finalModel isConfiguration:nil
compatibleWithStoreMetadata:sourceMetadata]) {
if (NULL != error) {
*error = nil;
}
return YES;
}
NSManagedObjectModel *sourceModel = [self sourceModelForSourceMetadata:sourceMetadata];
NSManagedObjectModel *destinationModel = nil;
NSMappingModel *mappingModel = nil;
NSString *modelName = nil;
if (![self getDestinationModel:&destinationModel
mappingModel:&mappingModel
modelName:&modelName
forSourceModel:sourceModel
error:error]) {
return NO;
}
// 我们现在有了一个映射模型,开始迁移
NSURL *destinationStoreURL = [self destinationStoreURLWithSourceStoreURL:sourceStoreURL
modelName:modelName];
NSMigrationManager *manager = [[NSMigrationManager alloc] initWithSourceModel:sourceModel
destinationModel:destinationModel];
if (![manager migrateStoreFromURL:sourceStoreURL
type:type
options:nil
withMappingModel:mappingModel
toDestinationURL:destinationStoreURL
destinationType:type
destinationOptions:nil
error:error]) {
return NO;
}
// 现在迁移成功了,把文件备份一下以防不测
if (![self backupSourceStoreAtURL:sourceStoreURL
movingDestinationStoreAtURL:destinationStoreURL
error:error]) {
return NO;
}
// 现在数据模型可能还不是“最新”版,所以接着递归
return [self progressivelyMigrateURL:sourceStoreURL
ofType:type
toModel:finalModel
error:error];
}
参考:http://objccn.io/issue-4-7/