使用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. kmean算法C++实现

    kmean均值算法是一种最常见的聚类算法.算法实现简单,效果也比较好.kmean算法把n个对象划分成指定的k个簇,每个簇中所有对象的均值的平均值为该簇的聚点(中心). k均值算法有如下五个步骤: 随机 ...

  2. spring cloud连载第三篇之Spring Cloud Netflix

    1. Service Discovery: Eureka Server(服务发现:eureka服务器) 1.1 依赖 <dependency> <groupId>org.spr ...

  3. Ionic CLI 升级到最新版本

    由于Ionic 自身也在不停的更新当中, 所以开发者经常会遇到从官方的CLI 命令,在命令行窗口中执行出错的情况. 就比如我一个月之前安装的ionic 2.2.2版本,已不能使用最新的3.2.0 CL ...

  4. SQL中利用ROW_NUMBER()进行分页查询

    SELECT ContractName ,ContractNO, State,CreateDate FROM (SELECT ContractName ,ContractNO,CreateDate, ...

  5. ARC基本原理

    基本简介 ARC是Automatic Reference Counting(自动引用计数器)的简称. ARC是ios5.0引入的新特性,完全消除手动管理内存的繁琐,编译器会自动在适合的代码里面插入适当 ...

  6. 算法 - 排序数组中与x最近一点

    条件: a[j] + a[j+1] < x*2 int findClosestPoint(int x,int a []) { int res = 0; int j = 0; while(j< ...

  7. WPF流程图制作系列相关基础一

    WPF流程图制作相关基础一   需求是要通过wpf开发流程图,这个流程图是用户自行拖动配置.   使用过流程图的话,应该大体能想象出流程图拖动配置的样子.这里主要会涉及到的技术知识点就是 wpf拖动相 ...

  8. [翻译]Review——24 tips for React Native you probably want to know

    Post author: Albert Gao Post link: http://www.albertgao.xyz/2018/05/30/24-tips-for-react-native-you- ...

  9. Git 学习之git 起步(一)

    起步 本章介绍开始使用 Git 前的相关知识.我们会先了解一些版本控制工具的历史背景,然后试着让 Git 在你的系统上跑起来,直到最后配置好,可以正常开始开发工作.读完本章,你就会明白为什么 Git ...

  10. AGC008E:Next or Nextnext

    传送门 考虑转化成图论问题,\(i\) 向 \(p_i\) 连边,那么合法方案一定是形成了若干个简单环或自环 考虑一个环内的情况: 如果 \(a_i=p_i\),那么 \(i\) 向 \(a_i\) ...