iPhone Core Data Lightweight Migration: Can't merge models

泄露秘密 提交于 2019-12-29 07:12:11

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/4745886/iphone-core-data-lightweight-migration-cant-merge-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!