Created
December 27, 2013 17:17
-
-
Save ryanmasondavies/8149841 to your computer and use it in GitHub Desktop.
Opens up a Core Data store using a custom atomic type and migrates the store to another type. This would be useful for importing data, e.g for opening up a CSV file using a custom type and converting it an SQLite store. However, while this is easy and makes use of in-house components, it introduces more steps than are necessary for importing dat…
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
// grab the model: | |
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"StoreMigration" withExtension:@"momd"]; | |
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; | |
// create a new example store type: | |
NSPersistentStoreCoordinator *csvCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel]; | |
csvCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel]; | |
if (![csvCoordinator addPersistentStoreWithType:[SMGCSVStore type] configuration:nil URL:csvStoreURL options:nil error:&error]) { | |
NSLog(@"Couldn't add store: %@", [error localizedDescription]); | |
abort(); | |
} | |
// set up a context and check there are two events: | |
NSManagedObjectContext *csvContext = [[NSManagedObjectContext alloc] init]; | |
[csvContext setPersistentStoreCoordinator:csvCoordinator]; | |
NSFetchRequest *csvRequest = [[NSFetchRequest alloc] initWithEntityName:@"SMGEvent"]; | |
NSArray *csvEvents = [csvContext executeFetchRequest:csvRequest error:&error]; | |
if (csvEvents) { | |
NSLog(@"Loading from CSV store:"); | |
for (NSManagedObject *event in csvEvents) { | |
NSLog(@"Event date: %@", [event valueForKey:@"date"]); | |
} | |
} else { | |
NSLog(@"Couldn't retrieve events: %@", [error localizedDescription]); | |
abort(); | |
} | |
// try to migrate the CSV store over to SQLite: | |
NSPersistentStore *csvStore = [csvCoordinator persistentStoreForURL:csvStoreURL]; | |
if (![csvCoordinator migratePersistentStore:csvStore toURL:sqlStoreURL options:nil withType:NSSQLiteStoreType error:&error]) { | |
NSLog(@"Failed to migrate: %@", [error localizedDescription]); | |
abort(); | |
} | |
// save the new store to disk: | |
if (![csvContext save:&error]) { | |
NSLog(@"Failed to save: %@", [error localizedDescription]); | |
abort(); | |
} | |
// load up the store directly from the migrated SQLite store: | |
NSPersistentStoreCoordinator *sqlCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel]; | |
if (![sqlCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:sqlStoreURL options:nil error:&error]) { | |
NSLog(@"Couldn't add store: %@", [error localizedDescription]); | |
abort(); | |
} | |
// check it has two events: | |
NSManagedObjectContext *sqlContext = [[NSManagedObjectContext alloc] init]; | |
[sqlContext setPersistentStoreCoordinator:sqlCoordinator]; | |
NSFetchRequest *sqlRequest = [[NSFetchRequest alloc] initWithEntityName:@"SMGEvent"]; | |
NSArray *sqlEvents = [sqlContext executeFetchRequest:sqlRequest error:&error]; | |
if (sqlEvents) { | |
NSLog(@"Loading from SQL store:"); | |
for (NSManagedObject *event in sqlEvents) { | |
NSLog(@"Event date: %@", [event valueForKey:@"date"]); | |
} | |
} else { | |
NSLog(@"Couldn't retrieve events: %@", [error localizedDescription]); | |
abort(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment