核心数据 - 轻量级迁移不起作用

Posted

技术标签:

【中文标题】核心数据 - 轻量级迁移不起作用【英文标题】:Core Data - Lightweight migration doesn't work 【发布时间】:2016-12-09 15:00:25 【问题描述】:

我是 Core Data 的新手,我目前正在使用 Core Data 维护一个应用程序。

由于需要尽快发布应用程序的新版本,我必须将 en Entity 添加到数据模型中。

我遵循了这个教程Lightweight tutorial,它非常有用,但我也阅读了所有 Apple 文档以及这篇精彩的文章 Core Data Migration,以便在全球范围内了解它是如何工作的。

虽然我只需要向数据模型添加一个实体,但我听说在这种情况下可以进行轻量级迁移。

只有 1 个新实体(没有属性)我必须链接到已经存在的父实体。

到目前为止我做了什么:

应用程序当前在数据模型的版本 3 上 我从版本 3 创建了一个新的数据模型(版本 4) 我已选择数据模型版本 4 作为当前数据模型 我创建了新实体(无属性),并将其链接到父实体。 我已经创建了生成的类对象 然后我修改了我的用户界面

构建并运行,它可以工作,很酷。但是当我从 AppStore 下载当前版本,并且当我从 TestFlight 安装新的最近制作的存档/IPA 时,(安装在旧版本上 -> 迁移场景)应用程序在没有我的新实体/数据模型的情况下运行。

从 Apple 文档中可以清楚地看出,Core Dara 支持添加实体进行轻量级迁移。

我知道这不是一个简单的过程,但我觉得我已经完美地遵循了一切。

如何在没有每次存档、在 TestFlight 上发布等情况下测试迁移...

如果您需要任何其他信息以便清楚地理解我的问题和/或写出更详细的答案,请随时在评论中提问,我将编辑我的问题。

提前谢谢你。

编辑:

这是来自 AppDelegate 的关于 NSPersistentStoreCoordinator 的代码。

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.
    if (_persistentStoreCoordinator != nil) 
        return _persistentStoreCoordinator;
    

    // Create the coordinator and store
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    NSURL *storeURL = [self persistentStoreURL];
    NSError *error = nil;
    NSString *failureReason = @"There was an error creating or loading the application's saved data.";
    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:@NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES error:&error]) 
        // Report any error we got.
        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:9999 userInfo:dict];
        // Replace this with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        DDLogError(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    

    return _persistentStoreCoordinator;

我不知道如何通过测试或日志有效地知道 CoreData 使用了新的数据模型(版本 4)并已成功执行迁移。

我知道当我从 xCode 构建时它可以工作,但它与来自 AppStore 的更新不同。

【问题讨论】:

【参考方案1】:

要为轻量级迁移设置应用程序,您需要将以下行插入到声明 Persistent Store 的 CoreData 框架中。这些设置启用了支持轻量级迁移的选项(这些来自 Swift 3.0 应用程序,因此如果您使用的是 2.3,它们可能会有所不同):

NSMigratePersistentStoresAutomaticallyOption as NSObject: true                 
NSInferMappingModelAutomaticallyOption as NSObject: true 

一旦这些行到位,CoreData 将在需要时正确执行轻量级迁移,包括添加新实体、属性和关系,因此只要您不执行任何需要更多操作的操作就可以了你的部分 - 比如更改实体或属性的名称。

【讨论】:

应用程序中已经使用了这些选项。该应用程序与我的新功能和新数据模型完美配合。但是在使用以前的数据模型安装应用程序时,它不会处理迁移。 尝试向新实体添加属性。如果迁移出现问题,您会收到一条错误消息,并且应用程序将无法运行,所以不是这样。我认为它只是没有触发迁移,因为实体在某种意义上看起来是“空的”。 好的,我向新实体添加了 Bool 属性。该应用程序没有崩溃,所以我的问题显然不是迁移问题。我尝试了 TestFlight、xCode 和 iTunes 等迁移过程。没有崩溃。我要结束这个问题,因为迁移已有效完成。 很高兴听到!我在文档中没有看到说明这是一个问题,但 API 有时需要实验......

以上是关于核心数据 - 轻量级迁移不起作用的主要内容,如果未能解决你的问题,请参考以下文章

核心数据自动迁移不起作用

多次传递的标准核心数据迁移不起作用

Grails 数据库迁移插件 updateOnStart 不起作用

启用迁移不起作用

Laravel 迁移不起作用

Laravel 5.4数据库迁移不起作用[重复]