CoreData 轻量级迁移

Core Data 的轻量级迁移可以处理对数据模型进行的简单更改,例如将新属性添加到现有实体中,轻量级迁移基本上与普通迁移相同,区别就是不需要映射模型,因为Core Data可以从数据模型推断映射.
      在轻量级迁移过程中,Core Data再NSBundle allBundles 结果返回的捆绑包中查找模型.出于这个原因,常见的做法是将数据模型存储再应用捆绑包中.

通过生成推断的映射模型执行轻量级迁移,模型更改必须满足以下条件:

1. 将新属性添加到现在实体中

2. 从实体中删除属性

3. 将属性从可选更改为非可选,反之亦然

4. 重命名实体

5. 重命名特性

6. 添加新关系

7. 删除现有关系

8. 重命名关系

重命名实体,关系或属性时,需要使用属性检测器将目标模型中指定的新名称设置为对应对象再源模型中的名称.

// CoreData 轻量级迁移
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
} _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreData_LightweightMigration.sqlite"];
NSError *error = nil;
NSString *failureReason = @"There was an error creating or loading the application's saved data."; /*! 主要添加的代码块*/
// NSMigratePersistentStoresAutomaticallyOption
// NSInferMappingModelAutomaticallyOption
// 设置为YES
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption,[NSNumber numberWithBool:YES],NSInferMappingModelAutomaticallyOption, nil]; if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) { NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
dict[NSLocalizedFailureReasonErrorKey] = failureReason;
dict[NSUnderlyingErrorKey] = error;
error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code: userInfo:dict]; NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
} // 去实现自动迁移,在运行时 Core Data必须能够发现它自己源和目的管理对象模型.如果你需要取把你的模型到本地,不通过自动检查发现,然后你需要去生成推测模型和初始化这个迁移你自己使用一个迁移管理(NSMigrationManager 实例) // 下面的简单代码简单的说明了怎么生成一个推测模型和使用迁移管理初始化一个迁移,这个代码假设你已经实现了两个方法 sourceModel和destinationModel , 分别的返回数据源和目的管理对象模型.
- (BOOL)migrateStore:(NSURL *)storeURL toVersionTwoStore:(NSURL *)dstStoreURL error:(NSError **)outError { // 1.创建原模型
NSManagedObjectModel * sourceModel = [NSManagedObjectModel mergedModelFromBundles:nil]; // 2.创建目的模型
NSManagedObjectModel * destinationModel = [NSManagedObjectModel mergedModelFromBundles:nil]; // Try to get an inferred mapping model.
// 3.尝试获得一个自动推测模型
NSMappingModel *mappingModel = [NSMappingModel inferredMappingModelForSourceModel:sourceModel destinationModel:destinationModel error:outError]; // If Core Data cannot create an inferred mapping model, return NO.
// 4.如果推测模型为空则返回NO
if (!mappingModel) {
return NO;
} // Create a migration manager to perform the migration.
// 5.创建一个推测管理实现迁移
NSMigrationManager *manager = [[NSMigrationManager alloc] initWithSourceModel:sourceModel destinationModel:destinationModel]; // 6. 管理迁移库从从储存URL到目的URL
BOOL success = [manager migrateStoreFromURL:storeURL type:NSSQLiteStoreType
options:nil withMappingModel:mappingModel toDestinationURL:dstStoreURL
destinationType:NSSQLiteStoreType destinationOptions:nil error:outError]; return success;
}

CoreData 轻量级迁移的更多相关文章

  1. CoreData数据库迁移的操作

    CoreData数据库迁移操作步骤,操作是基于Xcode7. 1.添加新的数据库.选中当前数据库版本:Editor->Add Model Verson,创建一个新的数据库版本. 2.Comman ...

  2. CoreData的简单使用(二)数据的增删改查,轻量级的版本迁移

    上一篇中我们已经使用CoreData创建了一个SQLite数据库 CoreData的简单使用(一)数据库的创建 现在对数据库进行数据的CRUD(增删改查) 1.Data Model 的设置 创建一个D ...

  3. CoreData的数据迁移

    CoreData的数据迁移   很多人说CoreData坑多,现在才感觉到,今天上午写代码的时候,发现了一个大问题. 过程如下,之前我自己给coredata的实体添加了几个字段,后来网上的一个用户说我 ...

  4. iOS CoreData (二) 版本升级和数据库迁移

    前言:最近ChinaDaily项目需要迭代一个新版本,在这个版本中CoreData数据库模型上有新增表.实体字段的增加,那么在用户覆盖安装程序时就必须要进行CoreData数据库的版本升级和旧数据迁移 ...

  5. 3.3. 轻量级的迁移方式(Core Data 应用程序实践指南)

    持久化存储协调器会试着用新版的模板打开原来的持久化存储区,但是那是旧的模板,旧的格式,当然会出错.现在要做的就是迁移现有的持久化数据区,以便跟新模型匹配. 怎么进行迁移呢? 在什么时候进行迁移? 在向 ...

  6. CoreData 数据模型的版本控制

    CoreData 数据模型的版本控制 在项目中选择数据模型,然后选择Editor | Add Model Version 通过属性栏的ModelVersion current 选项进行版本的选择控制. ...

  7. coreData详解

    1.初识CoreData CoreData的结构构成: NSManagedObjectModel的构成: 可以通过Entity创建继承自NSManagedObject类的文件,这个文件就是开发中使用的 ...

  8. 自定义 Core Data 迁移

    本文转载至 http://objccn.io/issue-4-7/ 感谢本文作者 朱宏旭 的不啬分享 自定义 Core Data 迁移似乎是一个不太起眼的话题.苹果在这方面只提供了很少的文档,若是初次 ...

  9. Core Data 迁移与版本管理

    原文  http://chun.tips/blog/2014/11/28/core-data-ban-ben-qian-yi-jing-yan-zong-jie/ 主题 Core DataiOS开发 ...

随机推荐

  1. iOS 模仿一个小项目,总结一下里边的模块

      ManoBoo:  参考链接:http://www.jianshu.com/p/fd4c46c31508  这个小的项目是参考ManoBoo的简书的,链接在上方,自己在仿做的过程中,也离不开Man ...

  2. apache2.2 做后端,增加真实ip到日志中

    apache2.2使用mod_remoteip模块 一.安装 wget https://github.com/ttkzw/mod_remoteip-httpd22/raw/master/mod_rem ...

  3. 汇编语言标记寄存器标记位_NV UP EI NG NZ AC PE CY

    在8086CPU中,有一种标记寄存器,长度为16bit: 其中存储的信息被称为程序状态字(Program Status Word,PSW),以下将该寄存器简称为flag. 功能:1)用来存储相关指令的 ...

  4. crontab执行时间和系统时间不一致

    最近发现一个非常奇怪的问题,症状如下: crontab 定时任务 配置时间 是 10 5 * * * (每日凌晨5点10分执行) 运行脚本时间却是 18:10左右 Dec 24 05:10:01 ht ...

  5. TID大会学习心得之软技能

    软技能(Personal development as a software developer) John Sonmez : Simple programmer的创始人 2.1 学习方法 学习不是一 ...

  6. Linux学习笔记

    性能问题排查: Linux系统出现了性能问题,一般我们可以通过top.iostat.free.vmstat等命令来查看初步定位问题.内存资源占用:free命令 IO占用:iostat -d -k 1 ...

  7. 关于smarty的一些个人笔记

    注释为{注释} 注意下面代码中<%extends file="路径"%>和<%widget name="路径"%>这两个路径中的区别 c ...

  8. New library for Matlab - test

    Thanks  http://www.matlabsky.com/thread-120-1-1.html Install directory $MatlabRoot\toolbox or >&g ...

  9. 去除magento多店铺URL地址中的“___from_store=”

    magento 的多店铺功能,大多数情况下是根据语言来进行选择的,当添加了多店铺之后,一般情况下我们会选择开启添加store code到url地址中. Magento 自带的这种功能算是比较不错了,但 ...

  10. 命名空间“System.Web”中不存在类型或命名空间名称“Optimization”(是否缺少程序集引用?)

    今天,在.net4.5,mvc4下新建了个区域,运行起来就报这个错误: 命名空间"System.Web"中不存在类型或命名空间名称"Optimization"( ...