From 16df3cc0bb5cb95d90dc7764cd4d2913c8fa15f5 Mon Sep 17 00:00:00 2001 From: Saul Mora Date: Tue, 18 Dec 2012 18:45:49 -0700 Subject: [PATCH] Add helpers to specify persistent stores and model files at specific URLs, not auto configured ones --- .../NSManagedObjectModel+MagicalRecord.h | 1 + .../NSManagedObjectModel+MagicalRecord.m | 5 +++++ ...PersistentStoreCoordinator+MagicalRecord.h | 3 +++ ...PersistentStoreCoordinator+MagicalRecord.m | 22 ++++++++++++++----- MagicalRecord/Core/MagicalRecord+Setup.h | 2 ++ MagicalRecord/Core/MagicalRecord+Setup.m | 10 +++++++++ 6 files changed, 38 insertions(+), 5 deletions(-) diff --git a/MagicalRecord/Categories/NSManagedObjectModel+MagicalRecord.h b/MagicalRecord/Categories/NSManagedObjectModel+MagicalRecord.h index 8a543b4..85b931d 100644 --- a/MagicalRecord/Categories/NSManagedObjectModel+MagicalRecord.h +++ b/MagicalRecord/Categories/NSManagedObjectModel+MagicalRecord.h @@ -15,6 +15,7 @@ + (void) MR_setDefaultManagedObjectModel:(NSManagedObjectModel *)newDefaultModel; ++ (NSManagedObjectModel *) MR_managedObjectModelAtURL:(NSURL *)url; + (NSManagedObjectModel *) MR_mergedObjectModelFromMainBundle; + (NSManagedObjectModel *) MR_newManagedObjectModelNamed:(NSString *)modelFileName NS_RETURNS_RETAINED; + (NSManagedObjectModel *) MR_managedObjectModelNamed:(NSString *)modelFileName; diff --git a/MagicalRecord/Categories/NSManagedObjectModel+MagicalRecord.m b/MagicalRecord/Categories/NSManagedObjectModel+MagicalRecord.m index cc4df40..f0ca5b4 100644 --- a/MagicalRecord/Categories/NSManagedObjectModel+MagicalRecord.m +++ b/MagicalRecord/Categories/NSManagedObjectModel+MagicalRecord.m @@ -27,6 +27,11 @@ static NSManagedObjectModel *defaultManagedObjectModel_ = nil; defaultManagedObjectModel_ = newDefaultModel; } ++ (NSManagedObjectModel *) MR_managedObjectModelAtURL:(NSURL *)url; +{ + return [[NSManagedObjectModel alloc] initWithContentsOfURL:url]; +} + + (NSManagedObjectModel *) MR_mergedObjectModelFromMainBundle; { return [self mergedModelFromBundles:nil]; diff --git a/MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.h b/MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.h index b4be8b7..fa23351 100644 --- a/MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.h +++ b/MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.h @@ -22,12 +22,15 @@ extern NSString * const kMagicalRecordPSCDidCompleteiCloudSetupNotification; + (NSPersistentStoreCoordinator *) MR_coordinatorWithSqliteStoreNamed:(NSString *)storeFileName; + (NSPersistentStoreCoordinator *) MR_coordinatorWithAutoMigratingSqliteStoreNamed:(NSString *)storeFileName; + (NSPersistentStoreCoordinator *) MR_coordinatorWithPersistentStore:(NSPersistentStore *)persistentStore; ++ (NSPersistentStoreCoordinator *) MR_coordinatorWithSqliteStoreAtURL:(NSURL *)url; + + (NSPersistentStoreCoordinator *) MR_coordinatorWithiCloudContainerID:(NSString *)containerID contentNameKey:(NSString *)contentNameKey localStoreNamed:(NSString *)localStoreName cloudStorePathComponent:(NSString *)subPathComponent; + (NSPersistentStoreCoordinator *) MR_coordinatorWithiCloudContainerID:(NSString *)containerID contentNameKey:(NSString *)contentNameKey localStoreNamed:(NSString *)localStoreName cloudStorePathComponent:(NSString *)subPathComponent completion:(void(^)(void))completionHandler; - (NSPersistentStore *) MR_addInMemoryStore; - (NSPersistentStore *) MR_addAutoMigratingSqliteStoreNamed:(NSString *) storeFileName; +- (NSPersistentStore *) MR_addSqliteStoreAtURL:(NSURL *)url withOptions:(NSDictionary *__autoreleasing)options; - (NSPersistentStore *) MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__autoreleasing NSDictionary *)options; - (void) MR_addiCloudContainerID:(NSString *)containerID contentNameKey:(NSString *)contentNameKey localStoreNamed:(NSString *)localStoreName cloudStorePathComponent:(NSString *)subPathComponent; diff --git a/MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m b/MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m index 05047ab..8b11b92 100644 --- a/MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m +++ b/MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m @@ -62,13 +62,17 @@ NSString * const kMagicalRecordPSCDidCompleteiCloudSetupNotification = @"kMagica } } -- (NSPersistentStore *) MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__autoreleasing NSDictionary *)options + +- (NSPersistentStore *) MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__autoreleasing NSDictionary *)options; { NSURL *url = [storeFileName isKindOfClass:[NSURL class]] ? storeFileName : [NSPersistentStore MR_urlForStoreName:storeFileName]; - NSError *error = nil; - + return [self MR_addSqliteStoreAtURL:url withOptions:options]; +} + +- (NSPersistentStore *) MR_addSqliteStoreAtURL:(NSURL *)url withOptions:(NSDictionary *__autoreleasing)options; +{ [self MR_createPathToStoreFileIfNeccessary:url]; - + NSError *error = nil; NSPersistentStore *store = [self addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url @@ -264,7 +268,7 @@ NSString * const kMagicalRecordPSCDidCompleteiCloudSetupNotification = @"kMagica { NSManagedObjectModel *model = [NSManagedObjectModel MR_defaultManagedObjectModel]; NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model]; - + [psc MR_addSqliteStoreNamed:[persistentStore URL] withOptions:nil]; return psc; @@ -284,6 +288,14 @@ NSString * const kMagicalRecordPSCDidCompleteiCloudSetupNotification = @"kMagica return [self MR_coordinatorWithSqliteStoreNamed:storeFileName withOptions:nil]; } ++ (NSPersistentStoreCoordinator *) MR_coordinatorWithSqliteStoreAtURL:(NSURL *)url; +{ + NSManagedObjectModel *model = [NSManagedObjectModel MR_defaultManagedObjectModel]; + NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model]; + + [psc MR_addSqliteStoreAtURL:url withOptions:nil]; + return psc; +} @end diff --git a/MagicalRecord/Core/MagicalRecord+Setup.h b/MagicalRecord/Core/MagicalRecord+Setup.h index a92fa92..c990d6f 100644 --- a/MagicalRecord/Core/MagicalRecord+Setup.h +++ b/MagicalRecord/Core/MagicalRecord+Setup.h @@ -14,6 +14,8 @@ + (void) setupCoreDataStackWithInMemoryStore; + (void) setupAutoMigratingCoreDataStack; ++ (void) setupCoreDataStackWithStoreAtURL:(NSURL *)url; + + (void) setupCoreDataStackWithStoreNamed:(NSString *)storeName; + (void) setupCoreDataStackWithAutoMigratingSqliteStoreNamed:(NSString *)storeName; diff --git a/MagicalRecord/Core/MagicalRecord+Setup.m b/MagicalRecord/Core/MagicalRecord+Setup.m index 1d5c1fd..8efa94d 100644 --- a/MagicalRecord/Core/MagicalRecord+Setup.m +++ b/MagicalRecord/Core/MagicalRecord+Setup.m @@ -53,4 +53,14 @@ [NSManagedObjectContext MR_initializeDefaultContextWithCoordinator:coordinator]; } ++ (void) setupCoreDataStackWithStoreAtURL:(NSURL *)url; +{ + if ([NSPersistentStoreCoordinator MR_defaultStoreCoordinator] != nil) return; + + NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_coordinatorWithSqliteStoreAtURL:url]; + [NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:coordinator]; + + [NSManagedObjectContext MR_initializeDefaultContextWithCoordinator:coordinator]; +} + @end