iPhoneのコアデータから始めたばかりですが、軽量移行で問題が発生しました。
- 古いモデルに2つの新しいフィールドを追加しました
- モデルクラスファイルを再生成しました
- 新しいモデルバージョンを現在のバージョンとして作成
生成されたテンプレートの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>
ここで何が欠けていますか?
更新:問題は、モデルで見逃していたデフォルト値の属性であることが判明しました。