使用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. tomcat启动(二)org.apache.catalina.startup.Bootstrap分析

    /** * Bootstrap loader for Catalina. This application constructs a class loader * for use in loading ...

  2. Object-C语言Block的实现方式

    开场白 Block基本概念 中间态转换方法 Block编译后结果分析 Block运行时状态与编译状态对比   开场白   Object-C语言是对C语言的扩展,所以将OC源码进行编译的时候,会将OC源 ...

  3. npm run build之后生成的dist如何扔到服务器运行(npm run build之后如何本地运行)

    运行npm run build之后,会生成一个dist文件夹,里面的目录结构大概是这样的: 生成完的文件我们怎么来运行呢?直接在本地打开inde.html是无法运行的,打包的时候有提示: 构建文件应该 ...

  4. secureCRT的文件上传技巧

    现在我们经常会习惯性的使用windows系统,但现在开发项目和维护中经常都在使用linux服务器,以为它的性能更强.更精简. 学习大数据的同志们和维护后端的同志们,推荐一下secureCRT软件,用起 ...

  5. 零基础学C#算法(零基础学算法——C#版)

    今天本人正在看算法方面的书.作为高中数学忘得差不多的渣渣,实在无力.无奈找了本书,c语言写的,哎.我就把其中代码翻译成C#版好了.此坑能否填平,看我耐性和网络支持条件吧.有生之年能看完的话我会把整个项 ...

  6. Go 中包导入声明

    Go中的程序由软件包组成.通常,软件包依赖于其他软件包,或者内置于标准库或第三方的软件包.包需要先导入才能使用其导出的标识符.本文将翻译一篇国外的文章,用于介绍包导入的原理以及几种常用的导入方式. & ...

  7. Java反射拾遗

    定义:Java反射机制可以让我们在编译期(Compile Time)之外的运行期(Runtime)检查类,接口,变量以及方法的信息.反射还可以让我们在运行期实例化对象,调用方法,通过调用get/set ...

  8. 去除底部“自豪地采用 WordPress”版权信息----最后附最新版的删除方法!!

    footer.php get_template_part( 'template-parts/footer/site', 'info' );

  9. C Traps:运算

    位移 如果sizeof(int) = 4,那么下面的代码的结果是什么? int x=255; printf("%d", x>>34); 实际输出:63 在编译这个代码时 ...

  10. 【canvas系列】canvas实现“ 简单的Amaziograph效果”--画对称图【强迫症福利】

    标题很难引人入胜,先放个效果图好了 如果图片吸引不了你,那我觉得也就没啥看的了. demo链接: https://win7killer.github.io/demo_set/html_demo/can ...