バンドル内のすべてのモデルをマージするのではなく、使用する2つのモデル(モデル1とモデル2の新しいバージョン)を指定し、modelByMergingModelsを使用してそれらをマージしました。
これは正しくないようです。なぜモデルをマージするのですか?モデル2を使用し、モデル1からストアを移行します。
NSManagedObjectModelクラスリファレンスから
modelByMergingModels:
既存のモデルの配列から単一のモデルを作成します。
ソースモデル(モデル1 )で特別なことや特定のことを行う必要はありません。バンドルに含まれている限り、自動軽量移行プロセスがそれを検出して使用します。
自動軽量移行と比較してひどいパフォーマンスが見られたので、Xcodeで作成したマッピングモデルを放棄することをお勧めします。あなたのマイレージは変わるかもしれません、モデル間の私の変化はあなたのものとは異なります、しかし私は驚かないでしょう。バンドルに独自のマッピングモデルがある場合とない場合で、タイミングを試してください。
/* Inferred mapping */
NSError *error;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,nil];
NSPersistentStore *migratedStore = [persistentStoreCoordinator addPersistentStoreWithType:nil
configuration:nil
URL:self.storeURL
options:options
error:&error];
migrationWasSuccessful = (migratedStore != nil);
ソースモデルをロードしてnilでないことを確認することで、コードでソースモデルが使用可能であることを確認できます。
NSString *modelDirectoryPath = [[NSBundle mainBundle] pathForResource:@"YourModelName" ofType:@"momd"];
if (modelDirectoryPath == nil) return nil;
NSString *modelPath = [modelDirectoryPath stringByAppendingPathComponent:@"YourModelName"];
NSURL *modelFileURL = [NSURL fileURLWithPath:modelPath];
NSManagedObjectModel *modelOne = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelFileURL];
if (modelOne == nil) {
NSLog(@"Woops, Xcode lost my source model");
}
else {
[modelOne release];
}
これは、プロジェクトにリソース「YourModelName.xcdatamodeld」と「YourModelName.xcdatamodel」があることを前提としています。
また、そのモデルが既存の移行前の永続ストアと互換性があるかどうかを確認できます。
NSError *error;
NSDictionary *storeMeta = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:nil URL:self.storeURL error:&error];
if (storeMeta == nil) {
// Unable to read store meta
return NO;
}
BOOL isCompatible = [modelOne isConfiguration:nil compatibleWithStoreMetadata:storeMeta];
-storeURLこのコードは、永続ストアのロード元を指定するメソッドがあることを前提としています。