使用CoreData [3]
使用CoreData [3]

此篇幅介绍CoreData如何升级版本防止崩溃
把你之前创建的实体文件全部删除掉,把沙盒中的数据库文件删除掉,实体只保持一个,然后重新创建出实体文件.


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"%@", NSHomeDirectory()); // 创建两条记录
[self createStudent:@"YouXianMing" age:[NSNumber numberWithInt:]];
[self createStudent:@"QiuLiang" age:[NSNumber numberWithInt:]]; // 存储记录
[self saveContext]; // 设定要查询的实体
NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"Student"]; // 取出查询结果
NSArray *students = [[self managedObjectContext] executeFetchRequest:fetch error:nil];
[students enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
Student *student = obj;
NSLog(@"%@ %@", student.age, student.name);
}]; return YES;
} - (Student *)createStudent:(NSString *)name age:(NSNumber *)age
{
// 实体描述信息
NSEntityDescription *description = \
[NSEntityDescription entityForName:@"Student"
inManagedObjectContext:[self managedObjectContext]]; // 初始化对象
Student *student = [[Student alloc] initWithEntity:description
insertIntoManagedObjectContext:[self managedObjectContext]];
student.name = name;
student.age = age; return student;
}
执行上述代码后运行结果为:
2014-07-31 09:13:40.424 StudyCoreData[16612:60b] 26 YouXianMing
2014-07-31 09:13:40.425 StudyCoreData[16612:60b] 27 QiuLiang
检查下数据库,此时只有两个属性哦:

然后我们给这个Student表新增加一个属性然后进行数据库的版本升级.
首先,我们先创建新的数据库表版本.



然后切换版本:


然后给Student新增加一个属性值




然后,我们来修改下源码试验测试结果:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSLog(@"%@", NSHomeDirectory()); // 存储记录
[self saveContext]; // 设定要查询的实体
NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"Student"]; // 取出查询结果
NSArray *students = [[self managedObjectContext] executeFetchRequest:fetch error:nil];
[students enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
Student *student = obj;
NSLog(@"%@ %@ %@", student.age, student.name, student.sex);
}]; return YES;
}
运行后会崩溃,提示不兼容.
这个时候我们需要修改点东西:
找到以下地方.

将nil替换成如下的形式.

然后运行.

崩溃问题解决了,版本升级成功哦:)
解决问题出处:
http://stackoverflow.com/questions/8881453/the-model-used-to-open-the-store-is-incompatible-with-the-one-used-to-create-the
Deleting the app is sometimes not the case! Suggest, your app has already been published! You can't just add new entity to the data base and go ahead - you need to perform migration!
For those who doesn't want to dig into documentation and is searching for a quick fix:
- Open your .xcdatamodeld file
- click on Editor
- select Add model version...
- Add a new version of your model (the new group of datamodels added)
- select the main file, open file inspector (right-hand panel)
- and under
Versioned core data modelselect your new version of data model for current data model - THAT'S NOT ALL ) You should perform so called "light migration".
- Go to your
AppDelegateand find where thepersistentStoreCoordinatoris being created - Find this line
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) - Replace
niloptions with@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}(actually provided in the commented code in that method) - Here you go, have fun!
P.S. This only applies for lightweight migration. For your migration to qualify as a lightweight migration, your changes must be confined to this narrow band:
- Add or remove a property (attribute or relationship).
- Make a nonoptional property optional.
- Make an optional attribute nonoptional, as long as you provide a default value.
- Add or remove an entity.
- Rename a property.
- Rename an entity.
使用CoreData [3]的更多相关文章
- iOS基本数据库存储方式 - CoreData
CoreData 创建模型文件的过程 1.选择模板 2.添加实体 3.添加实体的属性[注意]属性的首字母必须小写 一.CoreData管理类(必备以下三个类对象) 1.CoreData数据操作的上下文 ...
- iOS CoreData 中 objectID 的不变性
关于 CoreData的 objectID 官方文档有这样的表述:新建的Object还没保存到持久化存储上,那么它的objectID是临时id,而保存之后,就是持久化的id,不会再变化了. 那么,我想 ...
- CoreData __ 基本原理
操作过程 Context想要获取值,先要告诉连接器,我要什么东西 链接器再告诉store, 你给我什么东西, store去找 找到之后返回给链接器,链接器再返回给Context Co ...
- iOS CoreData primitive accessor
Given an entity with an attribute firstName, Core Data automatically generates firstName, setFirstNa ...
- 初识CoreData与详解
Core Data数据持久化是对SQLite的一个升级,它是iOS集成的,在说Core Data之前,我们先说说在CoreData中使用的几个类. (1)NSManagedObjectModel(被管 ...
- CoreData教程
网上关于CoreData的教程能搜到不少,但很多都是点到即止,真正实用的部分都没有讲到,而基本不需要的地方又讲了太多,所以我打算根据我的使用情况写这么一篇实用教程.内容将包括:创建entity.创建r ...
- CoreData和SQLite多线程访问时的线程安全
关于CoreData和SQLite多线程访问时的线程安全问题 数据库读取操作一般都是多线程访问的.在对数据进行读取时,我们要保证其当前状态不能被修改,即读取时加锁,否则就会出现数据错误混乱.IOS中常 ...
- IOS数据存储之CoreData使用优缺点
前言: 学习了Sqlite数据之后认真思考了一下,对于已经习惯使用orm数据库的开发者或者对sql语句小白的开发者来说该如何做好数据库开发呢?这个上网搜了一下?看来总李多虑了!apple 提供了一种数 ...
- iOS开发之表视图爱上CoreData
在接触到CoreData时,感觉就是苹果封装的一个ORM.CoreData负责在Model的实体和sqllite建立关联,数据模型的实体类就相当于Java中的JavaBean, 而CoreData的功 ...
- CoreData
之前在学习使用SQLite时, 需要编写大量的sql语句,完成数据的增删改查,但对于不熟悉sql语句的开发人员来说,难度较大,调试程序比较困难. 由此出现CoreData框架,将sql的操作转换成为对 ...
随机推荐
- caffe, caffe2, paddlepaddle, tensorflow对于cuda,cudnn,protobuf依赖的纠葛
由于在学习神经网络,为了尝试各种深度学习框架,电脑上目前安装了caffe, caffe2, paddlepaddle, tensorflow三款主流框架,但是安装过程中真是痛不欲生. 且不说单单安装一 ...
- 终极 shell zsh
在mac上安装zsh,推荐安装. 参见http://macshuo.com/?p=676. 安装成功提示,看着很帅的样子
- MVC及MVC Core在filter中如何获取控制器名称和Action名称
很多时候我们需要使用过滤器来实现一些拦截.验证等行为,此时我们能获取到的Context是ActionExecutingContext ,我们如何通过这个Context来获得Action.Control ...
- ES6 笔记(二)- 总结
在最近进行的项目中,已经全面使用到ES6,这里对ES6进行整理总结.用得比较多的是带*的内容,这些语法.新增类型.模块调用等从代码量上.可读性上.操作上给项目带来了不少便利. 1.语法 1 ...
- 虚拟机安装Linux中常见异常及解决办法
如果接着下去的提示按Test 的话 会出现Unable to read the disc checksum from the primary volume descriptor. This proba ...
- [转]Pass a ViewBag instance to a HiddenFor field in Razor
本文转自:https://stackoverflow.com/questions/27456983/pass-a-viewbag-instance-to-a-hiddenfor-field-in-ra ...
- C#基础知识回顾--BackgroundWorker介绍
简介 BackgroundWorker是.net里用来执行多线程任务的控件,它允许编程者在一个单独的线程上执行一些操作.耗时的操作(如下载和数据库事务)在长时间运行时可能会导致用户界面 (UI) 始终 ...
- Java中Date与String的相互转换
我们在注册网站的时候,往往需要填写个人信息,如姓名,年龄,出生日期等,在页面上的出生日期的值传递到后台的时候是一个字符串,而我们存入数据库的时候确需要一个日期类型,反过来,在页面上显示的时候,需要从数 ...
- 关于PLSQL启动用时较长的问题解决
问题: 打开登陆界面缓慢. 解决: 1.删除控制面板中的打印机 2.将打印机改为手动并停止启动状态 .
- SpringBoot+Mybatis+Generator 逆向工程使用(二)
Mybatis-Genarator 逆向工程使用 个人开发环境 java环境:Jdk1.8.0_60 编译器:IntelliJ IDEA 2017.1.4 mysql驱动:mysql-connecto ...