iPhone核心数据轻量级迁移:无法合并模型

发布于 2024-10-12 19:43:36 字数 2727 浏览 3 评论 0原文

我刚开始接触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>

我在这里缺少什么?

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

I just started with iPhone core data and I ran into a problem in lightweight migration.

  • I added two new fields to my old model
  • Regenerated the model class files
  • Made the new model version as current version
  • Added the following code in AppDelegate in the template generated

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

  • Then at last, I did a clean build before running the app.

I get the following error when the app crashes...

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

Now to debug I added the following code...

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

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

This displays the following result...

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;
}

I checked the app bundle > MyApp.momd > VersionInfo.plist file

its got the following contents...

<?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>

What am I missing here?

UPDATE: The problem turned out to be a default value attribute that I had missed in the model.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情域 2024-10-19 19:43:36

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

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

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

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

您可能想查看这篇文章

You might try forcing Core Data to infer a mapping model:

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

If the changes to your model were trivial, then Core Data may be able to infer a mapping model. If that fails, then you will probably need to create a mapping model (and revert to the options that you are currently using).

Mapping models are easy to create. Be mindful, though, if you change a data model then you will need to update the mapping, too.

You might want to check out this SO post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文