Created
August 8, 2012 17:15
-
-
Save binarybucks/3296730 to your computer and use it in GitHub Desktop.
Multi-Store NSPersistentStoreCoordinator
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
- (NSManagedObjectModel *)managedObjectModel | |
{ | |
if (__managedObjectModel != nil) | |
{ | |
return __managedObjectModel; | |
} | |
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"SupersetModel" withExtension:@"mom"]; | |
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; | |
return __managedObjectModel; | |
} | |
- (NSURL *)applicationDocumentsDirectory | |
{ | |
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); | |
NSString *documentPath = [searchPaths lastObject]; | |
return [NSURL fileURLWithPath:documentPath]; | |
} | |
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator | |
{ | |
if (__persistentStoreCoordinator != nil) | |
{ | |
return __persistentStoreCoordinator; | |
} | |
NSURL *dynURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"appname/dynStore.sqlite"]; | |
NSURL *persURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"appname/persStore.sqlite"]; | |
NSLog(@"dynURL %@", dynURL); | |
NSLog(@"persURL %@", persURL); | |
NSError *error1 = nil; | |
NSError *error2 = nil; | |
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; | |
// 1. Add "static", read-only store | |
/* | |
* FAILS WITH: | |
* Error Domain=NSCocoaErrorDomain Code=134100 "The managed object model version used to open the persistent store is incompatible with the one that was used to create | |
* the persistent store." UserInfo=0x101374540 {metadata={ | |
* NSPersistenceFrameworkVersion = 407; | |
* NSStoreModelVersionHashes = { | |
* PersistentEnity1 = <2d9d5e0b d7f6f74d 918cb9af f0fe44a4 fae60105 8144ccd0 112d0729 cead71eb>; | |
* PersistentEnity2 = <e31f4b20 a3c302b6 7b2a74e1 2737e8cb d440ae9a 5bfb503b 32330cdd 17a98978>; | |
* PersistentEnity2 = <0ef429b0 41998c3a 496ab11a 26bde9ac 7d1a929f 9d7ea102 9ece131d b5a6890c>; | |
* }; | |
* NSStoreModelVersionHashesVersion = 3; | |
* NSStoreModelVersionIdentifiers = ( | |
* "" | |
* ); | |
* NSStoreType = SQLite; | |
* NSStoreUUID = "9FDD6F54-6734-43B9-A71B-8AC09F710F5D"; | |
* "_NSAutoVacuumLevel" = 2; | |
* }, reason=The model used to open the store is incompatible with the one used to create the store} | |
*/ | |
[__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType | |
configuration:@"PersConfig" | |
persURL | |
options:@{ | |
NSReadOnlyPersistentStoreOption: @(YES), | |
NSInferMappingModelAutomaticallyOption : @(YES) | |
} | |
error:&error1]; | |
if (error1 != nil) { | |
NSLog(@"1: %@", [error1 description]); | |
return nil; | |
} | |
// 2. Add "dynamic", writable content | |
[__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType | |
configuration:@"DynConfig" | |
dynURL | |
options:@{ | |
NSMigratePersistentStoresAutomaticallyOption: @(YES), | |
NSInferMappingModelAutomaticallyOption : @(YES) | |
} | |
error:&error1]; | |
if (error2 != nil) { | |
NSLog(@"2: %@", [error2 description]); | |
return nil; | |
} | |
return __persistentStoreCoordinator; | |
} | |
- (NSManagedObjectContext *)managedObjectContext | |
{ | |
if (__managedObjectContext != nil) | |
{ | |
return __managedObjectContext; | |
} | |
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; | |
if (coordinator != nil) | |
{ | |
__managedObjectContext = [[NSManagedObjectContext alloc] init]; | |
[__managedObjectContext setPersistentStoreCoordinator:coordinator]; | |
} | |
return __managedObjectContext; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment