【问题标题】:iPhone Core Data Lightweight Migration: Can't merge modelsiPhone核心数据轻量级迁移:无法合并模型
【发布时间】:2011-06-12 08:47:06
【问题描述】:

我刚开始使用 iPhone 核心数据,在轻量级迁移中遇到了问题。

  • 我在旧模型中添加了两个新字段
  • 重新生成模型类文件
  • 将新模型版本设为当前版本
  • 在生成的模板中的AppDelegate中添加如下代码

    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]) {

  • 最后,我在运行应用程序之前做了一个干净的构建。

应用崩溃时出现以下错误...

The operation couldn’t be completed. (Cocoa error 134140.)" UserInfo=0x622b350 {reason=Can't find or automatically infer mapping model for migration

现在要调试,我添加了以下代码...

    NSError *error = nil;
    NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:storeURL error:&error];

    if (!sourceMetadata) {
        NSLog(@"sourceMetadata is nil");
    } else {
        NSLog(@"sourceMetadata is %@", sourceMetadata);
    }

这将显示以下结果...

2011-01-20 18:18:41.018 MyApp[4438:207] sourceMetadata is {
    NSPersistenceFrameworkVersion = 248;
    NSStoreModelVersionHashes =     {
        Fugitive = <e33370b6 e7ca3101 f91d2595 1e8bfe01 3e7fb4de 6ef2a31d 9e50237b b313d390>;
    };
    NSStoreModelVersionHashesVersion = 3;
    NSStoreModelVersionIdentifiers =     (
    );
    NSStoreType = SQLite;
    NSStoreUUID = "E711F65F-3C5A-4889-872B-6541E4B2863A";
    "_NSAutoVacuumLevel" = 2;
}

我检查了应用程序包 > MyApp.momd > VersionInfo.plist 文件

它有以下内容...

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>NSManagedObjectModel_CurrentVersionName</key>
    <string>MyApp 2</string>
    <key>NSManagedObjectModel_VersionHashes</key>
    <dict>
        <key>MyApp</key>
        <dict>
            <key>Fugitive</key>
            <data>
            4zNwtufKMQH5HSWVHov+AT5/tN5u8qMdnlAje7MT05A=
            </data>
        </dict>
        <key>MyApp 2</key>
        <dict>
            <key>Fugitive</key>
            <data>
            N58Lf4BNpACzrsHAb1+BQImgjsBZ+u5G0wGUyt84+Ec=
            </data>
        </dict>
    </dict>
</dict>
</plist>

我在这里错过了什么?

更新:问题原来是我在模型中遗漏的默认值属性。

【问题讨论】:

  • 当您使用属性列表编辑器打开 VersionInfo.plist 时,Fugitive 数据项是什么样的?在原始 XML 中,似乎是 64 位编码的 - 这使得它们很难(至少对我而言)与元数据调用提供的十六进制表示进行比较。
  • '4zNwtufKMQH5HSWVHov+AT5/tN5u8qMdnlAje7MT05A=' == 匹配您的“我的应用”模型。这很好。

标签: iphone core-data core-data-migration mapping-model


【解决方案1】:

您可以尝试强制 Core Data 推断映射模型:

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

如果对模型的更改是微不足道的,那么 Core Data 可能能够推断出映射模型。如果失败,那么您可能需要创建一个映射模型(并恢复为您当前使用的选项)。

映射模型很容易创建。但请注意,如果您更改数据模型,那么您也需要更新映射。

您可能想查看this SO post

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多