Core Dataを使用するすべての人は、「ストアを開くために使用されるモデルは、ストアを作成するために使用されるモデルと互換性がありません」というメッセージを知っています。
次に、シミュレーターからアプリを削除して、再構築する必要があります。
私の質問は、アプリv 1.0を送信してから、v 1.1のコアデータにいくつかのエンティティを追加する場合です。これは、1.1に更新した1.0のユーザーがデータをクリアすることを意味しますか?
モデルの新しいモデルバージョンを作成し、データベースを移行する必要があります。モデルの変更が必要な変更の範囲内である場合は、軽量の移行を実行できます。そうでない場合は、コアデータにデータの移行方法を指示する必要があります。移行ドキュメントを確認してください:http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreDataVersioning/Articles/Introduction.html
あなたの場合、古いデータ モデルの単純な拡張のように思えます。本当にいくつかの新しいエンティティまたは新しいクラスを追加するだけの場合は、いわゆる軽量移行が適切な方法です。
実際、この場合、ほとんど何もする必要はありませんが、元のモデルに加えて 2 番目のモデルを作成します。両方のモデルがあることが重要です。そうすれば、アプリは最初のバージョンと新しいバージョンを問題なくロードします。
新しいモデルを新しいモデルとしてマークすることを忘れないでください!
モデルの削除は非常に面倒なので、新しいモデルを作成するときは注意してください。
コードは次のようになります。
-(NSManagedObjectContext *)managedObjectContext {
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *lC = [self persistentStoreCoordinator];
if (lC != nil) {
managedObjectContext =[[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator: lC];
}
return managedObjectContext;
}
- (NSPersistentStoreCoordinator *) persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
// Allow inferred migration from the original version of the application.
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"DBName.sqlite"]];
NSError *error = nil;
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl
options:options error:&error]){
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
return persistentStoreCoordinator;
}
- (NSManagedObjectModel *) managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
return managedObjectModel;
}