おそらく、Marcus Zarra の Core Data ブックを入手して、移行について読む必要があります (Ch. 5)。しかし、そうでなくても、知っておくと便利な基本事項がいくつかあります。まず、更新されたアプリに古いモデル (スキーマ) と新しいモデルの両方が必要です。次に、新しいモデルが「現在のモデル」としてタグ付けされていることを確認する必要があります。3 番目に、既存のモデル (ディスクからロードされたもの) から新しいモデルに自動的にマップされるように、NSPersistentStoreCoordinator を作成する必要があります。
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (persistentStoreCoordinator)
return persistentStoreCoordinator;
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"MyDataStore.sqlite"]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
// Use mapping model
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, nil];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeUrl
options:options
error:&error])
{
[NSApp presentError:error];
}
return persistentStoreCoordinator;
}
更新
新しいアプリの古いモデルは、古いアプリのモデルとまったく同じである必要があります。これが当てはまるかどうかわからない場合は、確認するために実行できる手順がいくつかあります。私のやり方は少し複雑ですが、役に立つと思われる場合は、その概要を説明します。