Copyright (C) iPhoneアプリ開発備忘録 All rights reserved.
ブログ内で記したコード、内容の正確性は保証いたしません。
記載内容を実装したことにより発生した不具合・損害等の責任は一切負いません。



2012年05月22日

Core Data 自動マイグレーション on Xcode4.3



Core Dataでデータモデルを変更した場合に実施するマイグレーションを、Xcode4.3で組み込む方法。

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

posted by mobileDeveloper at 19:53 | Comment(0) | TrackBack(0) | Core Data はてなブックマーク - Core Data 自動マイグレーション on Xcode4.3 | このブログの読者になる | 更新情報をチェックする
この記事へのコメント
コメントを書く
お名前:

メールアドレス:

ホームページアドレス:

コメント:

認証コード: [必須入力]


※画像の中の文字を半角で入力してください。
※ブログオーナーが承認したコメントのみ表示されます。

この記事へのトラックバック
Apple、Appleのロゴ、App Store、iPodのロゴ、iTunesは、米国および他国のApple Inc.の登録商標です。
iPhone、iPod touch、iPadはApple Inc.の商標です。
iPhone商標は、アイホン株式会社のライセンスに基づき使用されています。
その他、本ブログに記載されている製品名、会社名は、それぞれ各社の商標または登録商標です。
×

この広告は90日以上新しい記事の投稿がないブログに表示されております。