Core Data修改了Data Model文件后报错
2016-07-13 09:49
369 查看
报错内容:“The model used to open the store is incompatible with the one used to create the store”
“用于打开仓库的模型与创建仓库的模型不匹配”
出现上述异常的原因是 :我们修改了Data Model文件,增加了新的Entity,包括Attribute属性、Relationship关系等等。
解决办法:
(1) 最简单的办法是,在iOS 5 或者 6的模拟器中,删除App,然后重新运行就可以了。
(2) 第二种办法是使用Data Model 数据模型的版本属性:
给Data Model(也就是被管理对象模型)添加一个新的版本:
在项目导航栏中,选择Data Model 文件;
选择 Editor > Create Model Version 菜单项;
在对话框中,输入Data Model文件的名称,并点击Save按钮;
然后设置 Data Model的当前版本号:
在项目导航栏中,选择Data Model文件;
在File inspector 面板窗口,找到Versioned Core Data Model 节点;
从弹出菜单中,选择当前模板版本号;
如果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.删除原来的类文件,重新生成下类。(这一步没有做)
在appdelegate中
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]) {
NSLog(@"failed to add persistent store with type to persistent store coordinator");
}
添加 *optionsDictionary,原来options:nil 改成options:optionsDictionary
5.重新编译下程序
“用于打开仓库的模型与创建仓库的模型不匹配”
出现上述异常的原因是 :我们修改了Data Model文件,增加了新的Entity,包括Attribute属性、Relationship关系等等。
解决办法:
(1) 最简单的办法是,在iOS 5 或者 6的模拟器中,删除App,然后重新运行就可以了。
(2) 第二种办法是使用Data Model 数据模型的版本属性:
给Data Model(也就是被管理对象模型)添加一个新的版本:
在项目导航栏中,选择Data Model 文件;
选择 Editor > Create Model Version 菜单项;
在对话框中,输入Data Model文件的名称,并点击Save按钮;
然后设置 Data Model的当前版本号:
在项目导航栏中,选择Data Model文件;
在File inspector 面板窗口,找到Versioned Core Data Model 节点;
从弹出菜单中,选择当前模板版本号;
如果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.删除原来的类文件,重新生成下类。(这一步没有做)
在appdelegate中
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]) {
NSLog(@"failed to add persistent store with type to persistent store coordinator");
}
添加 *optionsDictionary,原来options:nil 改成options:optionsDictionary
5.重新编译下程序
相关文章推荐
- Drawerlayout,ActionBarDrawetoggle,NavigationView使用
- JAVA反射机制
- Nandflash镜像尾部不应填充0xFF
- C# string bytes互转
- iOS书摘之Objective-C编程之道 iOS设计模式解析
- iOS开发支付集成之银联支付
- 大学两年来学到的东西和五月到六月学到的东西
- Java随机数的生成
- HelloWorld
- CodeForces 570B Simple Game
- 多态和虚函数详解
- UIGraphicsBeginImageContext系列知识
- IOS Dev Intro - GCD Series 01
- 布局的一点总结
- μC/OS-Ⅱ之任务设计
- 实现根据身高计算标准体重的难点
- 328. Odd Even Linked List
- Linux下必须知道的网络命令(持续更新)
- $PWD 和 $(pwd)
- iOS开发支付集成之微信支付