1. 新データモデルの作成
(1) Xcodeで、対象となるデータモデルを選択した状態にして、メニューバーから「Editor → Add Model Version」を選択。
(2) "Version name"に新しいデータモデル名を入力、"Based on model"に元となるデータモデルを選択。
(3) Finishボタンをクリックして、既存のデータモデルの並びに新しいデータモデルが作成されているのを確認する。
(4) 新しいデータモデルを開き、変更個所を記述する。
2. カレントデータモデルの切換え
(1) ***.xdatamodeldのトップレベルを選択する。
上記1.操作で、下記のような構成になる。
***.xdatamodeld // ←このレベルを選択する。
***Ver2.xdatamodel // 新データモデル(名称の"Ver2"は上記1.(2)で入力した任意の名前)
***.xdatamodel // 旧データモデル
(2) File inspectorを開き、Versioned Core Data Model項目のCurentのリストから、新データモデルを選択する。新しいデータモデルにグリーンのチェックマークアイコンが付いているのを確認する。
これで、新データモデルがアプリのカレント(デフォルト)のデータモデルとして適用される。
3. マッピングモデルの作成
(1) メニューバーから、「File → New → File...」を選択、Core DataのMapping Modelを選択して、Nextボタンをクリックする。
(2) "Please choose the Source Data Model for this Mapping Model"と表示されたダイアログ画面で、移行元のデータモデルを選択して、Nextボタンをクリックする。
(3) "Please choose the Target Data Model for this Mapping Model"と表示されたダイアログ画面で、移行先のデータモデルを選択して、Nextボタンをクリックする。
(4) 次に表示された画面で、マッピングモデルのファイル名を入力して、Createボタンをクリックしてマッピングモデルを作成する。
(5) プロジェクトに、"((4)で入力した名称).xcmappingmodel" の名称でマッピングモデルが作成されていることを確認する。
(6) (5)のマッピングモデルを開き、変更内容を確認。必要があれば内容を修正する。
4. AppDelegate.mファイルを修正し、自動マイグレーション処理を記述する。
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"DataModel.sqlite"];
/*
Set up the store.
For the sake of illustration, provide a pre-populated default store.
*/
NSFileManager *fileManager = [NSFileManager defaultManager];
// If the expected store doesn't exist, copy the default store.
if (![fileManager fileExistsAtPath:storePath]) {
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"DataModel" ofType:@"sqlite"];
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL];
}
}
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
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]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
Typical reasons for an error here include:
* The persistent store is not accessible
* The schema for the persistent store is incompatible with current managed object model
Check the error message to determine what the actual problem was.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return persistentStoreCoordinator;
}
・赤文字は追加個所
・青文字は変更個所
参考記事:
http://www.yoheim.net/blog.php?q=20120511
http://blog.cyz.jp/?p=281
http://xcatsan.blogspot.jp/2010/05/coredata_26.html
http://cocoadays.blogspot.jp/2011/02/coredata-coredatamanager.html
http://hippos-lab.com/blog/node/367
http://cocoadays.blogspot.jp/2010/12/iosmac-coredata-1-nsentitymigrationpoli.html
http://ken-plus.blogspot.jp/2012/03/core-data-model.html
http://blog.the-nerd.be/2012/02/how_to_do_a_lightweight_core_data_migration/
http://www.yoheim.net/blog.php?q=20120511