Skip to content

Instantly share code, notes, and snippets.

@edom18
Created February 5, 2014 12:07
Show Gist options
  • Save edom18/8822349 to your computer and use it in GitHub Desktop.
Save edom18/8822349 to your computer and use it in GitHub Desktop.
CoreDataのマイグレーションをしようとしたときのメモ(未解決あり) ref: http://qiita.com/edo_m18/items/717fe32d744a10df7179
//自動マイグレーション用にオプションを指定
NSDictionary *options = @{NSInferMappingAutomaticallyOption:@YES,
NSMigratePersistentStoresAutomaticallyOption:@YES};
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
//Error!
}
- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error
{
NSManagedObjectContext *context = [manager destinationContext];
NSString *entityName [mapping destinationEntityName];
NSManagedObject *dInstance = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context];
[dInstance setValue:@"value1" forKey:@"anyKey1"];
[dInstance setValue:@"value2" forKey:@"anyKey2"];
//…
[dInstance setValue:[sInstance valueForKey:@"existsValue1" forKey:@"existsKey1"];
[dInstance setValue:[sInstance valueForKey:@"existsValue2" forKey:@"existsKey2"];
//…
return YES;
}
- (BOOL)createDestinationInstancesForSourceInstance:(NSManagedObject *)sInstance entityMapping:(NSEntityMapping *)mapping manager:(NSMigrationManager *)manager error:(NSError **)error
{
NSManagedObjectContext *context = [manager destinationContext];
NSString *entityName [mapping destinationEntityName];
NSManagedObject *dInstance = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context];
[dInstance setValue:@"value1" forKey:@"anyKey1"];
[dInstance setValue:@"value2" forKey:@"anyKey2"];
//…
[dInstance setValue:[sInstance valueForKey:@"existsValue1" forKey:@"existsKey1"];
[dInstance setValue:[sInstance valueForKey:@"existsValue2" forKey:@"existsKey2"];
//…
return YES;
}
//自動マイグレーション用にオプションを指定
NSDictionary *options = @{NSInferMappingAutomaticallyOption:@YES,
NSMigratePersistentStoresAutomaticallyOption:@YES};
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
//Error!
}
//meta dataを取得する
NSDictionary *sourceMetaData = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:storeURL error:&error];
//コンテキストに適合性を問い合わせる
BOOL isCompatible = [_managedObjectContext isConfiguration:nil compatibleWithStoreMetadata:sourceMetaData];
if (!isCompatible) {
//マイグレーションが必要
}
//meta dataを取得する
NSDictionary *sourceMetaData = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:storeURL error:&error];
//コンテキストに適合性を問い合わせる
BOOL isCompatible = [_managedObjectContext isConfiguration:nil compatibleWithStoreMetadata:sourceMetaData];
if (!isCompatible) {
//マイグレーションが必要
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment