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

标签 ios objective-c core-data core-data-migration

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

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

我已经遵循了本教程 Lightweight tutorial这非常有用,而且我已经阅读了所有 Apple 文档,还阅读了这篇精彩的文章 Core Data Migration以了解全局范围内它是如何运作的。

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

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

到目前为止我做了什么:

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

构建并运行,它可以工作,很酷。但是,当我从 AppStore 下载当前版本,并且从 TestFlight 安装最近制作的新 Archive/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 更新的情况不同。

最佳答案

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

NSMigratePersistentStoresAutomaticallyOption as NSObject: true                 
NSInferMappingModelAutomaticallyOption as NSObject: true 

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

关于ios - 核心数据——轻量级迁移不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41063213/

相关文章:

objective-c - cocoa 和休息 : Should API details go in the model or in an API class?

ios - 从 CoreData 获取后,单元格中的 UIView 显示错误的颜色

ios - ios中的jwplayer未捕获异常

objective-c - NSView 子类中的可选文本

ios - 核心数据中的文件夹结构

ios - 为什么图像未显示在 Objective-C 的 cellforRowAtIndexPath 中

ios - 在 Swift 应用程序中更新本地 JSON 数据文件的最佳实践?

iphone - iOS 中图像查看器应用程序的策略

ios - Swift:函数调用之间的区别

ios - 创建 collectionViewCell 时出现异常