Created
February 5, 2014 12:07
-
-
Save edom18/8822349 to your computer and use it in GitHub Desktop.
CoreDataのマイグレーションをしようとしたときのメモ(未解決あり) ref: http://qiita.com/edo_m18/items/717fe32d744a10df7179
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//自動マイグレーション用にオプションを指定 | |
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! | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//自動マイグレーション用にオプションを指定 | |
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! | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//meta dataを取得する | |
NSDictionary *sourceMetaData = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType URL:storeURL error:&error]; | |
//コンテキストに適合性を問い合わせる | |
BOOL isCompatible = [_managedObjectContext isConfiguration:nil compatibleWithStoreMetadata:sourceMetaData]; | |
if (!isCompatible) { | |
//マイグレーションが必要 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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