使用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:

  1. Open your .xcdatamodeld file
  2. click on Editor
  3. select Add model version...
  4. Add a new version of your model (the new group of datamodels added)
  5. select the main file, open file inspector (right-hand panel)
  6. and under Versioned core data model select your new version of data model for current data model
  7. THAT'S NOT ALL ) You should perform so called "light migration".
  8. Go to your AppDelegate and find where the persistentStoreCoordinator is being created
  9. Find this line if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
  10. Replace nil options with @{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} (actually provided in the commented code in that method)
  11. 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]的更多相关文章

  1. iOS基本数据库存储方式 - CoreData

    CoreData 创建模型文件的过程 1.选择模板 2.添加实体 3.添加实体的属性[注意]属性的首字母必须小写 一.CoreData管理类(必备以下三个类对象) 1.CoreData数据操作的上下文 ...

  2. iOS CoreData 中 objectID 的不变性

    关于 CoreData的 objectID 官方文档有这样的表述:新建的Object还没保存到持久化存储上,那么它的objectID是临时id,而保存之后,就是持久化的id,不会再变化了. 那么,我想 ...

  3. CoreData __ 基本原理

    操作过程 Context想要获取值,先要告诉连接器,我要什么东西 链接器再告诉store, 你给我什么东西, store去找 找到之后返回给链接器,链接器再返回给Context          Co ...

  4. iOS CoreData primitive accessor

    Given an entity with an attribute firstName, Core Data automatically generates firstName, setFirstNa ...

  5. 初识CoreData与详解

    Core Data数据持久化是对SQLite的一个升级,它是iOS集成的,在说Core Data之前,我们先说说在CoreData中使用的几个类. (1)NSManagedObjectModel(被管 ...

  6. CoreData教程

    网上关于CoreData的教程能搜到不少,但很多都是点到即止,真正实用的部分都没有讲到,而基本不需要的地方又讲了太多,所以我打算根据我的使用情况写这么一篇实用教程.内容将包括:创建entity.创建r ...

  7. CoreData和SQLite多线程访问时的线程安全

    关于CoreData和SQLite多线程访问时的线程安全问题 数据库读取操作一般都是多线程访问的.在对数据进行读取时,我们要保证其当前状态不能被修改,即读取时加锁,否则就会出现数据错误混乱.IOS中常 ...

  8. IOS数据存储之CoreData使用优缺点

    前言: 学习了Sqlite数据之后认真思考了一下,对于已经习惯使用orm数据库的开发者或者对sql语句小白的开发者来说该如何做好数据库开发呢?这个上网搜了一下?看来总李多虑了!apple 提供了一种数 ...

  9. iOS开发之表视图爱上CoreData

    在接触到CoreData时,感觉就是苹果封装的一个ORM.CoreData负责在Model的实体和sqllite建立关联,数据模型的实体类就相当于Java中的JavaBean, 而CoreData的功 ...

  10. CoreData

    之前在学习使用SQLite时, 需要编写大量的sql语句,完成数据的增删改查,但对于不熟悉sql语句的开发人员来说,难度较大,调试程序比较困难. 由此出现CoreData框架,将sql的操作转换成为对 ...

随机推荐

  1. jieba分词加入特殊字符和空格

    将原始jieba字典中的空格更改为 @@ 1.原来:雅不可攀 3 nr 2.现在:雅不可攀@@3@@nr 将自定义用户词典加入的词与词性中间的空格更改为@@ 1.原来:牵连关系 50 n 2.现在:牵 ...

  2. freeSWITCH之多平台测试通信

    开始测试使用 强烈建议在统一的局域网下进行配置,通信 本机IP:192.168.1.155 架构 freeSWITCH搭建在以Windows平台作为通信服务器.fs_cli为服务器上测试客户端. X- ...

  3. maven 中 jar管理

    慢慢补充 将本地jar添加到maven的仓库中 mvn install:install-file -DgroupId=org.geotools -DartifactId=gt-swing -Dvers ...

  4. WPF样式动画Trigger.EnterActions和Trigger.ExitActions(ExitActions其实可以不做任何事情)

    这是一个鼠标移入后,控件往左移动的动画: <Style TargetType="{x:Type StackPanel}"> <Setter Property=&q ...

  5. Linux中终端和控制台的一些不成熟的理解

    首先声明,这仅仅是在下一些不成熟的想法.是通过看网上的一些资料和自己实践的一些心得,应该都是些很不成熟甚至是不太正确的想法.但是我还是想记录下来,算是一个心路历程吧.等以后成熟了,再来修改. 首先说一 ...

  6. java面试⑦高级部分

    2.6.2说一下Linux下面的一些常用命令 常用: pwd 获取当前路径 cd 跳转到目录 su -u 切换到管理员 ls 列举目录 文件操作命令: 文件 tail 查看 rm -rf 删除 vim ...

  7. Visual Studio最常用的快捷键

    Ctrl + J:快捷提示,强迫智能感知: Ctrl + 空格键:使用 IntelliSense(智能感知)自动完成: Ctrl + Z:撤销,回退键: Ctrl + Shift + 空格:强迫显示参 ...

  8. sqlserver把任意一列放到第一列并顺序排列

    请用一句sql写出将id为1234放到表的第一列,其他紧随其后并以正序排列的查询语句. 答案: select * from table where ID=2union all select * fro ...

  9. git 拉取远程分支报错(fatal: '' is not a commit and a branch '' cannot be created from it)

    问题描述从远程git上拉取某一个分支,然后报错,拉取不了这个分支. 拉取分支的命令: git checkout -b xxx-static-19 origin/xxx-static-19 其中xxx- ...

  10. Linux 添加定时任务,crontab -e 命令与直接编辑 /etc/crontab 文件

    1. 使用 crontab -e 命令编辑定时任务列表 使用这个命令编辑的定时任务列表是属于用户级别的,初次编辑后在 /var/spool/cron 目录下生成一个与用户名相同的文件,文件内容就是我们 ...