Skip to content

Instantly share code, notes, and snippets.

@saiday
Last active January 4, 2016 16:09
Show Gist options
  • Save saiday/8645235 to your computer and use it in GitHub Desktop.
Save saiday/8645235 to your computer and use it in GitHub Desktop.
Lightweight Core Data migration setup
- (void)defineCoreData
{
// Complete Core Data stack initialization
[managedObjectStore createPersistentStoreCoordinator];
NSDictionary *options = @{
NSMigratePersistentStoresAutomaticallyOption : @YES,
NSInferMappingModelAutomaticallyOption : @YES
};
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:[self propertyFromPlist:@"Settings" withKey:@"db_name"]];
NSString *seedPath = [[NSBundle mainBundle] pathForResource:@"RKSeedDatabase" ofType:@"sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [self.managedObjectStore addSQLitePersistentStoreAtPath:storePath
fromSeedDatabaseAtPath:seedPath
withConfiguration:nil
options:options
error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);
// Create the managed object contexts
[self.managedObjectStore createManagedObjectContexts];
// Configure a managed object cache to ensure we do not create duplicate objects
self.managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:self.managedObjectStore.persistentStoreManagedObjectContext];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment