10.5 Leopard以上を対象とするNSPersistentDocumentベースのCore Dataアプリケーションがあります。私はデータモデルを変更する更新をリリースしようとしているので、既存のドキュメントを新しいモデルに移行する必要があります。変更は比較的簡単で、私はそれらのマッピングモデルを作成しました。私は軽量マイグレーションを自動化しようとはしていません。私はマッピングモデルを持っています(LightweightマイグレーションはLeopardではサポートされていませんが、私のモデルの変更はそれを排除します)。次のように私のNSPersistentDocumentサブクラスでは、私は-configurePersistentStoreCoordinatorForURL...
を上書き:Mac OS X 10.5では10.6または10.7では自動コアデータの移行が失敗する
- (BOOL)configurePersistentStoreCoordinatorForURL:(NSURL *)url
ofType:(NSString *)fileType
modelConfiguration:(NSString *)configuration
storeOptions:(NSDictionary *)storeOptions
error:(NSError **)error
{
NSMutableDictionary *newOptions = (storeOptions ?
[NSMutableDictionary dictionaryWithDictionary:storeOptions] :
[NSMutableDictionary dictionary]);
[newOptions setObject:(id)kCFBooleanTrue forKey:NSMigratePersistentStoresAutomaticallyOption];
return [super configurePersistentStoreCoordinatorForURL:url
ofType:fileType
modelConfiguration:configuration
storeOptions:newOptions
error:error];
}
これは10.6と10.7で正常に動作します。ただし、10.5では[super configurePersistentStore...]
の呼び出しは例外をスローし、失敗します。エラーは次のとおりです。
Error Domain=NSCocoaErrorDomain Code=134020 UserInfo=0x15812d70 "The model configuration used to open the store is incompatible with the one that was used to create the store."
私が代わりにこのコードを使用して、手動で移行を開始した場合:
NSArray *bundles = [NSArray arrayWithObject:[NSBundle mainBundle]];
NSManagedObjectModel *sourceModel = [NSManagedObjectModel mergedModelFromBundles:bundles forStoreMetadata:sourceMetadata];
NSManagedObjectModel *destinationModel = [psc managedObjectModel];
NSMappingModel *mappingModel = [NSMappingModel mappingModelFromBundles:bundles forSourceModel:sourceModel destinationModel:destinationModel];
NSMigrationManager *migrationManager = [[[NSMigrationManager alloc] initWithSourceModel:sourceModel destinationModel:destinationModel] autorelease];
BOOL migrationSuccessful = [migrationManager migrateStoreFromURL:backupURL
type:NSXMLStoreType
options:storeOptions
withMappingModel:mappingModel
toDestinationURL:url
destinationType:NSXMLStoreType
destinationOptions:storeOptions
error:error];
return [psc addPersistentStoreWithType:NSXMLStoreType configuration:configuration URL:url options:storeOptions error:error] != nil;
移行がOKに動作します。しかし、私は自動移行を使用することをお勧めします。なぜなら、他の理由がないと、よりクリーンなコードを作成するからです。誰も10.6以上で動作する自動移行で同様の問題が発生しましたが、10.5では発生しませんでしたか?私の感想は、組み込みのマイグレーションコードが何らかの理由でマッピングモデルを見つけることができないような、かなりシンプルなことですが、何が必要なのか分かりません。
まったく同じ問題と解決策がありました(マーケティング部がOS X 10.5のサポートを停止することに決めたので、手動移行コードを削除しました)。 – cdelacroix
私は自動移行機能/堅牢性が10.5以降で追加されていると思います... – nielsbot
私は彼らが持っていると確信していますが、私はそれが失敗すると思うほど複雑なことはしません。とにかく、次のリリースでは、このアプリは10.6+にすぎないので、もはや問題にはなりません。ある時点で現在の手動移行コードを削除します。しかし、なぜこれが起こったのかを知ることは素晴らしいことです。 –