iOS coredata 数据库升级 时报Can't find model for source store
在coredata 数据库结构被更改后,没根据要求立即建立新version,而是在原version上进行了小修改,之后才想起来建立新版本。并通过以下代码合并数据库,
NSError *error = nil;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,NSFileProtectionComplete, NSPersistentStoreFileProtectionKey, nil]; _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
结果出现了Can't find model for source store这个 bug。我们先看看stackoverflow上的答案
Your sqlite database's model hash MUST match one of the mom or momd created by your xcdatamodel when you build your app. You can see the hashes in the momd's VersionInfo.plist in the built app's bundle. See below for code to find your database's model hash. So if you change your xcdatamodel instead of creating a new version under Xcode->Editor->Add Model Version... then your model's hash will be different, and addPersistentStoreWithType won't be able to use your old database, which used the old model. That's what causes the "Can't find model for source store" error.
我们根据这个答案去看看bundle 中的version 文件
更改前的截图
再看更改后的截图
注意到 同样的version 6 中的profile model 的hash值不一致,程序会根据这个hash值来判断bundle中有无能在旧数据库上使用的mom文件,如果没有,就会报错。
为什么会crash呢,因为旧版更新为新版后,documents中的数据库是不变的,变化的是bundle中的.momd文件加中的mom文件和version的plist。新版本更新后,会参照新版本中包含的旧版的mom文件来读取旧版本数据库,向新版本迁移。如果旧的mom文件在新版本中更改了,程序就无法读取旧版本数据,就会抛出异常。
解决方法也很简单,找到上个版本使用的xxxx 6.xcdatamodel 文件,替换被错误更改的.xcdatamodel 文件。
iOS coredata 数据库升级 时报Can't find model for source store的更多相关文章
- CoreData数据库升级
如果IOS App 使用到CoreData,并且在上一个版本上有数据库更新(新增表.字段等操作),那在覆盖安装程序时就要进行CoreData数据库的迁移,具体操作如下: 1.选中你的mydata.xc ...
- iOS - CoreData 数据库存储
1.CoreData 数据库 CoreData 是 iOS SDK 里的一个很强大的框架,允许程序员以面向对象的方式储存和管理数据.使用 CoreData 框架,程序员可以很轻松有效地通过面向对象的接 ...
- iOS:CoreData数据库的使用四(数据库和UITableViewController以及NSFetchedResultsController一起使用)
CoreData数据库虽然可以和tableview或者UITableViewController一起使用将数据显示在表格上,但是在准备数据的时候,这种方式需要用一个可变数组来装从数据库一次性取出来的所 ...
- iOS:CoreData数据库的使用三(数据库和tableView表格一起使用)
CoreData数据库是用来持久性存储数据的,那么,我们再从该数据库中取出数据干什么呢?明显的是为了对数据做操作,这个过程中可以将它们直观的显示出来,即通过表格的形式显示出来.CoreData配合ta ...
- iOS:CoreData数据库的使用一(创建单个数据库表)
CoreData数据库框架:mac系统自带的数据库,它是苹果公司对sqlite进行封装而来的,既提供了对数据库的主要操作,也提供了具体的视图关系模型. 需要用到三个对象: 1•Managed Obje ...
- coredata 数据库升级
在真实开发中,因为需求是不断变化的,说不定什么时候就需要往模型里添加新的字段,添加新的模型,甚至是大规模的重构:所以数据的迁移就显得尤为重要了. CoreData 中,数据迁移本质就是把旧的 SQLi ...
- iOS coredata 数据库的加密(待研究)
https://github.com/project-imas/encrypted-core-data 使用起来很方便,底层还是使用了SQLCipher,有时间要研究一下! 数据库的密码不能用固定字符 ...
- CoreData 数据库
封装CoreManager类 @implementation CoreDataManager { //上下文 NSManagedObjectContext *_ctx; } //单例 +(instan ...
- CoreData(数据库升级 )版本迁移-iOS App升级安装
版权声明:本文为博主原创文章,未经博主允许不得转载. 如果IOS App 使用到CoreData,并且在上一个版本上有数据库更新(新增表.字段等操作),那在覆盖安装程序时就要进行CoreData数据库 ...
随机推荐
- Orchard源码分析(5.1):Host初始化(DefaultOrchardHost.Initialize方法)
概述 Orchard作为一个可扩展的CMS系统,是由一系列的模块(Modules)或主题(Themes)组成,这些模块或主题统称为扩展(Extensions).在初始化或运行时需要对扩展进行安装:De ...
- Java备份约9亿条数据
需求:有一张表9亿多条数据,数据加索引总数据量61GB.考虑到这张表的大部分数据都不会再被使用并且大数据量可能影响整库的性能,所以决定将表里某一个时刻之前的数据备份到一张新表中,待备份完成后将旧表中已 ...
- C# 对象 序列化 XML
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- CentOs图形界面的开启与关闭
1.1 shell中运行 init 3 进入文本模式,同时会关闭相关的服务(Xserver 肯定关闭) 1.2 Alt+Ctrl+F1~F6到字符界面,root登陆,ps aux|grep /usr ...
- C#委托全解析
什么是委托? 委托类似于C语 ...
- IOC和bean容器
- Java Sax解析
一. Java Sax解析是按照xml文件的顺序一步一步的来解析,在解析xml文件之前,我们要先了解xml文件的节点的种类,一种是ElementNode,一种是TextNode.如下面的这段boo ...
- lianjie
数值策划入门:如何确定游戏中的资源价值和定价http://bbs.gameres.com/thread_494366.html 一张常规的RPG游戏地图的制作流程 http://bbs.gameres ...
- springMVC-1
1.springMVC请求由前端到后端的流程 2.配置过程 (1)需要的jar包 spring-aop.jar spring-beans.jar spring-context.jar spring-c ...
- 修改Mysql默认编码
show variables like 'character%';+--------------------------+----------------------------+| Variable ...