Add support for specifying a configuration and customizing the options when adding a SQLite persistent store to the managed object store. closes #984

This commit is contained in:
Blake Watters
2012-10-29 00:32:56 -04:00
parent d11415b01e
commit c44f4010fa
7 changed files with 53 additions and 28 deletions

View File

@@ -129,11 +129,17 @@
@param storePath The path at which to save the persistent store on disk.
@param seedPath An optional path to a seed database to copy to the given storePath in the event that a store does not yet exist.
@param nilOrConfigurationName An optional name of a Core Data configuration in the managed object model.
@param nilOrOptions An optional dictionary of options with which to configure the persistent store. If `nil`, a dictionary of options enabling `NSMigratePersistentStoresAutomaticallyOption` and `NSInferMappingModelAutomaticallyOption` will be used.
@param error On input, a pointer to an error object. If an error occurs, this pointer is set to an actual error object containing the error information. You may specify nil for this parameter if you do not want the error information.
@warning If the seed database at the given path was created with an incompatible managed object model an application error may be raised.
*/
- (NSPersistentStore *)addSQLitePersistentStoreAtPath:(NSString *)storePath fromSeedDatabaseAtPath:(NSString *)seedPath error:(NSError **)error;
- (NSPersistentStore *)addSQLitePersistentStoreAtPath:(NSString *)storePath
fromSeedDatabaseAtPath:(NSString *)seedPath
withConfiguration:(NSString *)nilOrConfigurationName
options:(NSDictionary *)nilOrOptions
error:(NSError **)error;
/**
Resets the persistent stores in the receiver's persistent store coordinator and recreates them. If a store being reset is backed by a file on disk (such as a SQLite file), the file will be removed prior to recreating the store. If the store was originally created using a seed database, the seed will be recopied to reset the store to its seeded state.

View File

@@ -107,7 +107,11 @@ static RKManagedObjectStore *defaultStore = nil;
return [self.persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:error];
}
- (NSPersistentStore *)addSQLitePersistentStoreAtPath:(NSString *)storePath fromSeedDatabaseAtPath:(NSString *)seedPath error:(NSError **)error
- (NSPersistentStore *)addSQLitePersistentStoreAtPath:(NSString *)storePath
fromSeedDatabaseAtPath:(NSString *)seedPath
withConfiguration:(NSString *)nilOrConfigurationName
options:(NSDictionary *)nilOrOptions
error:(NSError **)error
{
if (! self.persistentStoreCoordinator) [self createPersistentStoreCoordinator];
@@ -117,14 +121,29 @@ static RKManagedObjectStore *defaultStore = nil;
if (! success) return nil;
}
// Allow inferred migration from the original version of the application.
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
(seedPath ? seedPath : [NSNull null]), RKSQLitePersistentStoreSeedDatabasePathOption,
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
nil];
NSDictionary *options = nil;
if (nilOrOptions) {
NSMutableDictionary *mutableOptions = [nilOrOptions mutableCopy];
mutableOptions[RKSQLitePersistentStoreSeedDatabasePathOption] = seedPath ?: [NSNull null];
options = mutableOptions;
} else {
options = @{ RKSQLitePersistentStoreSeedDatabasePathOption: (seedPath ?: [NSNull null]),
NSMigratePersistentStoresAutomaticallyOption: @(YES),
NSInferMappingModelAutomaticallyOption: @(YES) };
}
/**
There seems to be trouble with combining configurations and migration. So do this in two steps: first, attach the store with NO configuration, but WITH migration options; then remove it and reattach WITH configuration, but NOT migration options.
http://blog.atwam.com/blog/2012/05/11/multiple-persistent-stores-and-seed-data-with-core-data/
http://stackoverflow.com/questions/1774359/core-data-migration-error-message-model-does-not-contain-configuration-xyz
*/
NSPersistentStore *persistentStore = [self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:error];
if (! persistentStore) return nil;
if (! [self.persistentStoreCoordinator removePersistentStore:persistentStore error:error]) return nil;
return [self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:error];
NSDictionary *seedOptions = @{ RKSQLitePersistentStoreSeedDatabasePathOption: (seedPath ?: [NSNull null]) };
return [self.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nilOrConfigurationName URL:storeURL options:seedOptions error:error];
}
- (BOOL)copySeedDatabaseIfNecessaryFromPath:(NSString *)seedPath toPath:(NSString *)storePath error:(NSError **)error