Core Data 模型版本升级和数据迁移(二)轻量级数据迁移

文章来源:developer.apple.com html

轻量级数据迁移(下称LM), Core Data 自动执行,适用模型简单改变(simple changes),包括:实体/表中增长新属性/字段,LM 与普通迁移原理彻底同样,只是不须要映射模型(mapping model) (参见 “Mapping Overview”),Core Data 自行推测(infers)版本间的差别。


LM 适用于APP早期开发,那时老是在改变数据结构,同时没有必要保存一些测试数据。另外一个好处是,当你使用推测模型(inferred model)和 SQLite 存储时,Core Data 能够执行就地(in situ)迁移,严格讲,直接生成并执行 SQL 语句。由于没有载入任何数据,因此此时能保证效率。 数据结构

LM中 Core Data 必须能推测出映射关系


Core Data 须要可以在运行时找到源和目标的映射关系,通常咱们将数据模型存放在 bundles文件夹中(使用 NSBundle 的 allBundles 和 allFrameworks 方法得到)Core Data能够找到并发现它,若是放在别的地方,须要按照  “Use a Migration Manager if Models Cannot Be Found Automatically .” 中说的作。Core Data 随后自动分析现有表和字段的变化,自动产生推测的映射模型(inferred mapping model)。 并发

如下几种状况,Core Data 能够自动推测出: app

  • 添加新属性/字段 ide

  • 删除属性/字段 工具

  • 必填属性/字段改成可选属性/字段 测试

  • 可选属性/字段改成必填属性/字段,并设置默认值 ui

  • 重命名实体/表或属性/字段 spa

若是要重命名,须要将新版本中新实体/属性的“重命名标识符”(renaming identifier)的值设置为原来的实体名或属性名。具体方法:Xcode Data Modeling 工具->属性查看器(property inspector)。能够想象,这样作之后,如一个属性的名字在三次修改中都变化了,且都标记了重命名标识符,这样无论是从 version 2 到 version 3 仍是从 version 1 到 version 3 均可以无错误的迁移。 code

另外,Core Data 还能够自动猜想出:

  • 增长关系relationship)和 改变关系类型

    • 增长新关系,删除现有关系

    • 重命名关系名称 (一样设置重命名标识符)

    • 改变关系由对一到对多,或者a non-ordered to-many to ordered (反之亦然)

  • 改变实体/表的体系结构

    • 增长、删除、重命名实体/表

    • 增长新的父、子实体/表,移动属性/字段的顺序

    • 将实体/表移出体系

      注意,不能整合(merge)两个实体体系(if two existing entities do not share a common parent in the source, they cannot share a common parent in the destination)

使用选项字典(Options Dictionary)请求自动迁移(Automatic Migration)


定义选项字典,将 NSMigratePersistentStoresAutomaticallyOption 和 NSInferMappingModelAutomaticallyOption 两个键值设置为YES,而后调用 addPersistentStoreWithType:configuration:URL:options:error:

NSError *error = nil;
NSURL *storeURL = <#The URL of a persistent store#>;
NSPersistentStoreCoordinator *psc = <#The coordinator#>;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
    [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
 
BOOL success = [psc addPersistentStoreWithType:<#Store type#>
                    configuration:<#Configuration or nil#> URL:storeURL
                    options:options error:&error];
if (!success) {
    // Handle the error.
}

可使用 NSMappingModel 的 inferredMappingModelForSourceModel:destinationModel:error: 方法,获取推测的映射模型,若是能够推测出返回,若是不能够,返回 nil。

若是不能自动找到模型,则使用迁移管理器(Migration Manager)


执行自动迁移,必须保证 Core Data 可以在运行时找到源对象模型和目标对象模型,若是你须要将模型存放于自动搜索范围以外,那么须要咱们本身生成推测的模型,并实例化迁移管理器(NSMigrationManager)。以下面的代码,这些代码假设已经实现了sourceModel 和 destinationModel,这两个方法分别返回源对象和目标对象。

- (BOOL)migrateStore:(NSURL *)storeURL toVersionTwoStore:(NSURL *)dstStoreURL error:(NSError **)outError {
 
    // Try to get an inferred mapping model.
    NSMappingModel *mappingModel =
        [NSMappingModel inferredMappingModelForSourceModel:[self sourceModel]
                        destinationModel:[self destinationModel] error:outError];
 
    // If Core Data cannot create an inferred mapping model, return NO.
    if (!mappingModel) {
        return NO;
    }
 
    // Create a migration manager to perform the migration.
    NSMigrationManager *manager = [[NSMigrationManager alloc]
        initWithSourceModel:[self sourceModel] destinationModel:[self destinationModel]];
 
    BOOL success = [manager migrateStoreFromURL:storeURL type:NSSQLiteStoreType
        options:nil withMappingModel:mappingModel toDestinationURL:dstStoreURL
        destinationType:NSSQLiteStoreType destinationOptions:nil error:outError];
 
    return success;
}
相关文章
相关标签/搜索