Xcode 7 使用CoreData 关于数据库升级问题
来源:互联网 发布:淘宝助理一口价 编辑:程序博客网 时间:2024/05/17 09:26
coreData 很好用,但是也很坑
今天不说coreData 的具体使用,只提在实际运用中很关键的一种情况那就是数据库升级
以添加新字段为情形描述
先已有旧数据库,只有name age 两个字段
当你更新app版本时添加了一个新字段 address 这个时候如果直接将address 字段加入表内,运行就会崩溃
正确方法:
1.新建一个数据库分支 在Editor ->Add Model Version 新命名一个版本比如 coredatatest2
2.在工程的右边选择你新建的版本为当前版本
然后重新Editor ->Creat New NSMangeObject Subclass 这时新版本的数据模型就会将新字段关联上
3. 剩下一步是最关键的
修改- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 方法内容
添加代码
NSDictionary *options =
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
nil];
替换 条件
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options(这里以前是nil 现在替换为上面的options) 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();
}
这时候再运行代码可以观察数据库变化,新字段已经添加进去了
- Xcode 7 使用CoreData 关于数据库升级问题
- coredata 数据库升级
- Xcode升级后Coredata报错
- 关于升级xcode 5.1编译失败问题
- iOS App升级安装 - CoreData数据库升级
- iOS App升级安装 - CoreData数据库升级
- iOS App升级安装 - CoreData数据库升级
- iOS App升级安装 - CoreData数据库升级
- iOS App升级安装 - CoreData数据库升级
- iOS App升级安装 - CoreData数据库升级
- iOS App升级安装 - CoreData数据库升级
- 关于Xcode升级后插件无法使用
- CoreData的操作(使用, 升级)
- 升级xcode 7遇到的问题
- xcode 升级 插件问题
- 学习coredata 升级的时候问题
- ios CoreData的使用记录(XCode 4.2)
- CoreData在Xcode 8 中的使用
- 小明去银行取钱了(同步异步、阻塞与非阻塞)
- Struts2的ModelDriven
- ubuntu防火墙设置
- Android开发中使用AndroidStudio与Eclipse的代码区别----自定义控件时自定义属性
- [疯狂Java]AWT:右键菜单
- Xcode 7 使用CoreData 关于数据库升级问题
- struts2 表单验证
- Android Studio 告诉你名字,自己修改快捷键
- ORACLE 11G ADRCI 使用小记
- 位运算求组合
- Webpack 学习笔记
- Java设计模式之访问者模式
- 2016数学建模美赛总结反思
- hadoop 集群 加入一个新的存储节点和删除一个计算节点需要刷新集群状态命令