Last active
January 4, 2016 16:09
-
-
Save saiday/8645235 to your computer and use it in GitHub Desktop.
Lightweight Core Data migration setup
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
- (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