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;}
- iOS 使用CoreData Model Version版本升级处理
- iOS coredata的使用及版本升级
- iOS coredata的使用及版本升级
- iOS 版本迭代 App升级安装 CoreData数据库升级
- CoreData(数据库升级 )版本迁移-iOS App升级安装
- CoreData(数据库升级 )版本迁移-iOS App升级安装
- Coredata版本升级注意事项
- Coredata的版本升级
- ios coreData 根据应用版本升级进行更新
- iOS 使用CoreData处理大量数据
- iOS CoreData(Swift版本)
- java版本升级异常Unsupported major.minor version 52.0处理
- iOS App升级安装 - CoreData数据库升级
- iOS App升级安装 - CoreData数据库升级
- iOS App升级安装 - CoreData数据库升级
- iOS App升级安装 - CoreData数据库升级
- iOS App升级安装 - CoreData数据库升级
- iOS App升级安装 - CoreData数据库升级
- dp初步(poj1276)
- 第一篇博客
- Code Review 最佳实践
- iOS指纹识别(解锁)
- IOS开发中,SVN如何恢复到某一个版本(以Cornerstone为例)
- iOS 使用CoreData Model Version版本升级处理
- android开发Fragment not attached to Activity异常解决
- debug环境下打印
- 捕获Home按键
- Android内存泄漏检测-LeakCanary
- oracle中rollback的使用
- Java Web开发的轻便架构Tapestry5---页面渲染之框架职责
- Cordova/CDVViewController.h file not found 解决办法
- Linux 线程属性函数总结