iOS 使用CoreData Model Version版本升级处理

来源:互联网 发布:男士内裤品牌 知乎 编辑:程序博客网 时间:2024/05/28 23:12


一旦开发者了解到维持管理对象模型版本的简易,一些开发者不免会过分使用。这会产生一个过分复杂化的版本历史记录,如果每次更改都添加版本,这只会减缓模型的迁移。

在你发布Core Data app到App Store之前,你可以忽略版本控制,并按你喜欢的那样编辑模型。为避免“the store is incompatible”错误,可以简单地从开发设备上删除app,并再次在Xcode中运行。使用更新的模型部署一个新的持久化储存,就可以解决崩溃问题。一旦你把model version 1发布到App Store,你所有的用户将会有version 1的持久化存储。从这一点上来说,如果更新模型则必须添加一个新版本。我们假定你的用户正使用model version 1。当开发一个更新版的app,你已经添加了model versions 2, 3和4。使用以下小技巧可以减少版本历史,而不用发布model versions 2, 3,4…
删除model 2的内容
复制model 4内容至model 2
设置model 2为当前model
删除model 4

当然,你需要考虑model 1中的实体如何映射到更重要的model 2中,尤其在你没有使用轻量级迁移时。更加详细的关于model版本控制和迁移,可查看“Learning Core Data for iOS”这一个完整章节。


如果IOS App 使用到CoreData,并且在上一个版本上有数据库更新(新增表、字段等操作),那在覆盖安装程序时就要进行CoreData数据库的迁移,具体操作如下:

1.选中你的mydata.xcdatamodeld文件,选择菜单editor->Add Model Version  比如取名:mydata2.xcdatamodel
2.设置当前版本
   选择上级mydata.xcdatamodeld ,在inspector中的Versioned Core Data Model选择Current模版为mydata2

3.修改新数据模型mydata2,在新的文件上添加字段及表

4.删除原来的类文件,重新生成下类。

5.添加代码。到方法

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator中 添加 *optionsDictionary,原来options:nil  改成options:optionsDictionary

NSDictionary *optionsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],                                       NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES],                                       NSInferMappingModelAutomaticallyOption, nil];




- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {    // The persistent store coordinator for the application. This implementation creates and returns a coordinator, having added the store for the application to it.    if (_persistentStoreCoordinator != nil) {        return _persistentStoreCoordinator;    }        // Create the coordinator and store        _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Florens.sqlite"];    NSError *error = nil;    NSString *failureReason = @"There was an error creating or loading the application's saved data.";            NSDictionary *optionsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],                                       NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES],                                       NSInferMappingModelAutomaticallyOption, nil];    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:optionsDictionary error:&error]) {        // Report any error we got.        NSMutableDictionary *dict = [NSMutableDictionary dictionary];        dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";        dict[NSLocalizedFailureReasonErrorKey] = failureReason;        dict[NSUnderlyingErrorKey] = error;        error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];        // Replace this 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.        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);        abort();    }        return _persistentStoreCoordinator;}


1 0