Core Data 迁移与版本管理

  • 对数据模型进行版本管理
  • 数据迁移:不同版本数据模型之间进行转换的机制
  • 轻量级迁移
  • 标准迁移

1. 关于数据模型

  • 数据模型会被编译
    •   .xcdatamodel文件会编译成一种新的文件,其扩展名为.mom,它表示Managed Object Model。
  • 数据模型可以拥有多个版本
  • 创建新数据模型
    •   选中模型--》点击Editor菜单--》选择Add Model Version

2. 迁移

  • 轻量迁移
    •   用于相对简单的数据模型修改。(如简单的添加或移除实体中的属性,或添加移除实体)。
    • // 在设置持久化存储助手的时候进行一些修改
      if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]){}
      // 替换为
      NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption:@YES,NSInferMappingModelAutomaticallyOption:@YES};
      if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]){}
  • 标准迁移
    •   标准迁移包含创建映射模型,甚至需要编写代码来告诉Core Data如何将数据从旧的持久化存储移动到新的版本。
原文地址:https://www.cnblogs.com/chemandy/p/3590985.html