Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fix for #51 and #411

  • Loading branch information...
commit 77d706ec9ca481e0848713d772b2bf2d03219ddc 1 parent eb72053
Sebastian Hunkeler authored committed
View
7 MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.h
@@ -19,16 +19,19 @@ extern NSString * const kMagicalRecordPSCDidCompleteiCloudSetupNotification;
+ (NSPersistentStoreCoordinator *) MR_newPersistentStoreCoordinator NS_RETURNS_RETAINED;
++ (void)setWALJournallingEnabled:(BOOL)enabled;
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithSqliteStoreNamed:(NSString *)storeFileName;
++ (NSPersistentStoreCoordinator *) MR_coordinatorWithSqliteStore:(NSURL *)storeURL;
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithAutoMigratingSqliteStoreNamed:(NSString *)storeFileName;
++ (NSPersistentStoreCoordinator *) MR_coordinatorWithAutoMigratingSqliteStore:(NSURL *) storeURL;
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithPersistentStore:(NSPersistentStore *)persistentStore;
+ (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_addSqliteStoreNamed:(id)storeFileName withOptions:(__autoreleasing NSDictionary *)options;
+- (NSPersistentStore *) MR_addAutoMigratingSqliteStore:(NSURL *) storeURL;
+- (NSPersistentStore *) MR_addSqliteStore:(NSURL*)url withOptions:(__autoreleasing NSDictionary *)options;
- (void) MR_addiCloudContainerID:(NSString *)containerID contentNameKey:(NSString *)contentNameKey localStoreNamed:(NSString *)localStoreName cloudStorePathComponent:(NSString *)subPathComponent;
- (void) MR_addiCloudContainerID:(NSString *)containerID contentNameKey:(NSString *)contentNameKey localStoreNamed:(NSString *)localStoreName cloudStorePathComponent:(NSString *)subPathComponent completion:(void(^)(void))completionBlock;
View
51 MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m
@@ -8,6 +8,7 @@
#import "CoreData+MagicalRecord.h"
static NSPersistentStoreCoordinator *defaultCoordinator_ = nil;
+static BOOL walJournallingEnabled_ = YES;
NSString * const kMagicalRecordPSCDidCompleteiCloudSetupNotification = @"kMagicalRecordPSCDidCompleteiCloudSetupNotification";
@interface NSDictionary (MagicalRecordMerging)
@@ -62,9 +63,8 @@ - (void) MR_createPathToStoreFileIfNeccessary:(NSURL *)urlForStore
}
}
-- (NSPersistentStore *) MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__autoreleasing NSDictionary *)options
+- (NSPersistentStore *) MR_addSqliteStore:(NSURL*)url withOptions:(__autoreleasing NSDictionary *)options
{
- NSURL *url = [storeFileName isKindOfClass:[NSURL class]] ? storeFileName : [NSPersistentStore MR_urlForStoreName:storeFileName];
NSError *error = nil;
[self MR_createPathToStoreFileIfNeccessary:url];
@@ -130,10 +130,13 @@ - (NSPersistentStore *) MR_addInMemoryStore
+ (NSDictionary *) MR_autoMigrationOptions;
{
- // Adding the journalling mode recommended by apple
NSMutableDictionary *sqliteOptions = [NSMutableDictionary dictionary];
- [sqliteOptions setObject:@"WAL" forKey:@"journal_mode"];
+ if(walJournallingEnabled_){
+ // Adding the journalling mode recommended by apple
+ [sqliteOptions setObject:@"WAL" forKey:@"journal_mode"];
+ }
+
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
@@ -142,32 +145,43 @@ + (NSDictionary *) MR_autoMigrationOptions;
return options;
}
-- (NSPersistentStore *) MR_addAutoMigratingSqliteStoreNamed:(NSString *) storeFileName;
+- (NSPersistentStore *) MR_addAutoMigratingSqliteStore:(NSURL *) storeURL;
{
NSDictionary *options = [[self class] MR_autoMigrationOptions];
- return [self MR_addSqliteStoreNamed:storeFileName withOptions:options];
+ return [self MR_addSqliteStore:storeURL withOptions:options];
}
#pragma mark - Public Class Methods
++ (void)setWALJournallingEnabled:(BOOL)enabled
+{
+ walJournallingEnabled_ = enabled;
+}
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithAutoMigratingSqliteStoreNamed:(NSString *) storeFileName
{
+ NSURL *storeURL = [storeFileName isKindOfClass:[NSURL class]] ? (NSURL*) storeFileName : [NSPersistentStore MR_urlForStoreName:storeFileName];
+ return [self MR_coordinatorWithAutoMigratingSqliteStore:storeURL];
+}
+
++ (NSPersistentStoreCoordinator *) MR_coordinatorWithAutoMigratingSqliteStore:(NSURL *) storeURL
+{
NSManagedObjectModel *model = [NSManagedObjectModel MR_defaultManagedObjectModel];
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
- [coordinator MR_addAutoMigratingSqliteStoreNamed:storeFileName];
+ [coordinator MR_addAutoMigratingSqliteStore:storeURL];
//HACK: lame solution to fix automigration error "Migration failed after first pass"
- if ([[coordinator persistentStores] count] == 0)
+ if ([[coordinator persistentStores] count] == 0)
{
- [coordinator performSelector:@selector(MR_addAutoMigratingSqliteStoreNamed:) withObject:storeFileName afterDelay:0.5];
+ [coordinator performSelector:@selector(MR_addAutoMigratingSqliteStore:) withObject:storeURL afterDelay:0.5];
}
-
+
return coordinator;
}
+
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithInMemoryStore
{
NSManagedObjectModel *model = [NSManagedObjectModel MR_defaultManagedObjectModel];
@@ -220,7 +234,8 @@ - (void) MR_addiCloudContainerID:(NSString *)containerID contentNameKey:(NSStrin
}
[self lock];
- [self MR_addSqliteStoreNamed:localStoreName withOptions:options];
+ NSURL *storeURL = [localStoreName isKindOfClass:[NSURL class]] ? (NSURL*) localStoreName : [NSPersistentStore MR_urlForStoreName:localStoreName];
+ [self MR_addSqliteStore:storeURL withOptions:options];
[self unlock];
dispatch_async(dispatch_get_main_queue(), ^{
@@ -273,23 +288,29 @@ + (NSPersistentStoreCoordinator *) MR_coordinatorWithPersistentStore:(NSPersiste
NSManagedObjectModel *model = [NSManagedObjectModel MR_defaultManagedObjectModel];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
- [psc MR_addSqliteStoreNamed:[persistentStore URL] withOptions:nil];
+ [psc MR_addSqliteStore:persistentStore.URL withOptions:nil];
return psc;
}
-+ (NSPersistentStoreCoordinator *) MR_coordinatorWithSqliteStoreNamed:(NSString *)storeFileName withOptions:(NSDictionary *)options
++ (NSPersistentStoreCoordinator *) MR_coordinatorWithSqliteStore:(NSURL *)storeURL withOptions:(NSDictionary *)options
{
NSManagedObjectModel *model = [NSManagedObjectModel MR_defaultManagedObjectModel];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
- [psc MR_addSqliteStoreNamed:storeFileName withOptions:options];
+ [psc MR_addSqliteStore:storeURL withOptions:options];
return psc;
}
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithSqliteStoreNamed:(NSString *)storeFileName
{
- return [self MR_coordinatorWithSqliteStoreNamed:storeFileName withOptions:nil];
+ NSURL *storeURL = [storeFileName isKindOfClass:[NSURL class]] ? (NSURL*) storeFileName : [NSPersistentStore MR_urlForStoreName:storeFileName];
+ return [self MR_coordinatorWithSqliteStore:storeURL];
+}
+
++ (NSPersistentStoreCoordinator *) MR_coordinatorWithSqliteStore:(NSURL *)storeURL
+{
+ return [self MR_coordinatorWithSqliteStore:storeURL withOptions:nil];
}
@end
View
3  MagicalRecord/Core/MagicalRecord+Setup.h
@@ -17,5 +17,8 @@
+ (void) setupCoreDataStackWithStoreNamed:(NSString *)storeName;
+ (void) setupCoreDataStackWithAutoMigratingSqliteStoreNamed:(NSString *)storeName;
++ (void) setupCoreDataStackWithStore:(NSURL *)storeURL;
++ (void) setupCoreDataStackWithAutoMigratingSqliteStore:(NSURL *)storeURL;
+
@end
View
33 MagicalRecord/Core/MagicalRecord+Setup.m
@@ -23,14 +23,18 @@ + (void) setupAutoMigratingCoreDataStack
[self setupCoreDataStackWithAutoMigratingSqliteStoreNamed:[self defaultStoreName]];
}
++(void) populateDefaultStoreCoordinator:(NSPersistentStoreCoordinator*)coordinator
+{
+ [NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:coordinator];
+ [NSManagedObjectContext MR_initializeDefaultContextWithCoordinator:coordinator];
+}
+
+ (void) setupCoreDataStackWithStoreNamed:(NSString *)storeName
{
if ([NSPersistentStoreCoordinator MR_defaultStoreCoordinator] != nil) return;
NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_coordinatorWithSqliteStoreNamed:storeName];
- [NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:coordinator];
-
- [NSManagedObjectContext MR_initializeDefaultContextWithCoordinator:coordinator];
+ [self populateDefaultStoreCoordinator:coordinator];
}
+ (void) setupCoreDataStackWithAutoMigratingSqliteStoreNamed:(NSString *)storeName
@@ -38,19 +42,32 @@ + (void) setupCoreDataStackWithAutoMigratingSqliteStoreNamed:(NSString *)storeNa
if ([NSPersistentStoreCoordinator MR_defaultStoreCoordinator] != nil) return;
NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_coordinatorWithAutoMigratingSqliteStoreNamed:storeName];
- [NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:coordinator];
+ [self populateDefaultStoreCoordinator:coordinator];
+}
+
++ (void) setupCoreDataStackWithStore:(NSURL *)storeURL
+{
+ if ([NSPersistentStoreCoordinator MR_defaultStoreCoordinator] != nil) return;
- [NSManagedObjectContext MR_initializeDefaultContextWithCoordinator:coordinator];
+ NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_coordinatorWithSqliteStore:storeURL];
+ [self populateDefaultStoreCoordinator:coordinator];
+}
+
++ (void) setupCoreDataStackWithAutoMigratingSqliteStore:(NSURL *)storeURL
+{
+ if ([NSPersistentStoreCoordinator MR_defaultStoreCoordinator] != nil) return;
+
+ NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_coordinatorWithAutoMigratingSqliteStore:storeURL];
+ [self populateDefaultStoreCoordinator:coordinator];
}
+
+ (void) setupCoreDataStackWithInMemoryStore;
{
if ([NSPersistentStoreCoordinator MR_defaultStoreCoordinator] != nil) return;
NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_coordinatorWithInMemoryStore];
- [NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:coordinator];
-
- [NSManagedObjectContext MR_initializeDefaultContextWithCoordinator:coordinator];
+ [self populateDefaultStoreCoordinator:coordinator];
}
@end
Please sign in to comment.
Something went wrong with that request. Please try again.