Skip to content
Browse files

NSAtomicStore added

Now you can setup Core Data stack with manualy Store Type name. That
means you can use MagicalRecord with NSAtomicStore easily.
  • Loading branch information...
1 parent 477049d commit b30bb2b1eb2ee75057ff1a2e99de4badeb83e5c3 @ilin-in committed
View
4 MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.h
@@ -20,7 +20,9 @@ extern NSString * const kMagicalRecordPSCDidCompleteiCloudSetupNotification;
+ (NSPersistentStoreCoordinator *) MR_newPersistentStoreCoordinator NS_RETURNS_RETAINED;
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithSqliteStoreNamed:(NSString *)storeFileName;
++ (NSPersistentStoreCoordinator *) MR_coordinatorWithCustomStoreNamed:(NSString *)storeFileName storeType:(NSString*)storeType;
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithAutoMigratingSqliteStoreNamed:(NSString *)storeFileName;
++ (NSPersistentStoreCoordinator *) MR_coordinatorWithAutoMigratingCustomStoreNamed:(NSString *) storeFileName storeType:(NSString*) storeType;
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithPersistentStore:(NSPersistentStore *)persistentStore;
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithiCloudContainerID:(NSString *)containerID contentNameKey:(NSString *)contentNameKey localStoreNamed:(NSString *)localStoreName cloudStorePathComponent:(NSString *)subPathComponent;
@@ -28,7 +30,9 @@ extern NSString * const kMagicalRecordPSCDidCompleteiCloudSetupNotification;
- (NSPersistentStore *) MR_addInMemoryStore;
- (NSPersistentStore *) MR_addAutoMigratingSqliteStoreNamed:(NSString *) storeFileName;
+- (NSPersistentStore *) MR_addAutoMigratingCustomStoreNamed:(NSArray *) params;
- (NSPersistentStore *) MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__autoreleasing NSDictionary *)options;
+- (NSPersistentStore *) MR_addCustomStoreNamed:(id)storeFileName withOptions:(__autoreleasing NSDictionary *)options storeType:(NSString*)storeType;
- (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
66 MagicalRecord/Categories/NSPersistentStoreCoordinator+MagicalRecord.m
@@ -64,12 +64,18 @@ - (void) MR_createPathToStoreFileIfNeccessary:(NSURL *)urlForStore
- (NSPersistentStore *) MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__autoreleasing NSDictionary *)options
{
+ return [self MR_addCustomStoreNamed:storeFileName withOptions:options storeType:NSSQLiteStoreType];
+}
+
+// For use MagicalRecord with NSAtomicStore or other store types
+- (NSPersistentStore *) MR_addCustomStoreNamed:(id)storeFileName withOptions:(__autoreleasing NSDictionary *)options storeType:(NSString*)storeType
+{
NSURL *url = [storeFileName isKindOfClass:[NSURL class]] ? storeFileName : [NSPersistentStore MR_urlForStoreName:storeFileName];
NSError *error = nil;
[self MR_createPathToStoreFileIfNeccessary:url];
- NSPersistentStore *store = [self addPersistentStoreWithType:NSSQLiteStoreType
+ NSPersistentStore *store = [self addPersistentStoreWithType:storeType
configuration:nil
URL:url
options:options
@@ -82,11 +88,11 @@ - (NSPersistentStore *) MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__
{
// Could not open the database, so... kill it!
[[NSFileManager defaultManager] removeItemAtURL:url error:nil];
-
+
MRLog(@"Removed incompatible model version: %@", [url lastPathComponent]);
// Try one more time to create the store
- store = [self addPersistentStoreWithType:NSSQLiteStoreType
+ store = [self addPersistentStoreWithType:storeType
configuration:nil
URL:url
options:options
@@ -97,7 +103,7 @@ - (NSPersistentStore *) MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__
error = nil;
}
}
-
+
[MagicalRecord handleErrors:error];
}
return store;
@@ -141,21 +147,53 @@ - (NSPersistentStore *) MR_addAutoMigratingSqliteStoreNamed:(NSString *) storeFi
return [self MR_addSqliteStoreNamed:storeFileName withOptions:options];
}
+/**
+ @params Array of parameters:
+ - Store file name
+ - Store type name
+ */
+- (NSPersistentStore *) MR_addAutoMigratingCustomStoreNamed:(NSArray *) params;

Let's not make a dictionary of parameters :( This is not very discoverable.

@ilin-in Owner
ilin-in added a note

ok. What is better to use here define new class or struct? performSelector:withObject:afterDelay: doesn't accept 2 params :(

@ilin-in Owner
ilin-in added a note

I'm have fixed this issue in b30bb2b by replacing performSelector:withObject:afterDelay: to dispatch_after

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+{
+ if (params.count < 2) {
+ MRLog(@"Error: Argument count mismatch on '%s'.", __FUNCTION__);
+ }
+ NSDictionary *options = [[self class] MR_autoMigrationOptions];
+ return [self MR_addCustomStoreNamed:params[0] withOptions:options storeType:params[1]];
+}
#pragma mark - Public Class Methods
++ (NSPersistentStoreCoordinator *) MR_coordinatorWithAutoMigratingSqliteStoreNamed:(NSString *) storeFileName {
+ NSManagedObjectModel *model = [NSManagedObjectModel MR_defaultManagedObjectModel];
+ NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
+
+ [coordinator MR_addAutoMigratingSqliteStoreNamed:storeFileName];
+
+ //HACK: lame solution to fix automigration error "Migration failed after first pass"
+ if ([[coordinator persistentStores] count] == 0)
+ {
+ [coordinator performSelector:@selector(MR_addAutoMigratingSqliteStoreNamed:) withObject:storeFileName afterDelay:0.5];
+ }
+
+ return coordinator;
+}
-+ (NSPersistentStoreCoordinator *) MR_coordinatorWithAutoMigratingSqliteStoreNamed:(NSString *) storeFileName
++ (NSPersistentStoreCoordinator *) MR_coordinatorWithAutoMigratingCustomStoreNamed:(NSString *) storeFileName storeType:(NSString*) storeType
{
NSManagedObjectModel *model = [NSManagedObjectModel MR_defaultManagedObjectModel];
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
- [coordinator MR_addAutoMigratingSqliteStoreNamed:storeFileName];
+ if (!storeType) {
+ MRLog(@"Loading default SQLite store type");
+ storeType = NSSQLiteStoreType;
+ }
+
+ [coordinator MR_addAutoMigratingCustomStoreNamed:@[storeFileName, storeType]];
//HACK: lame solution to fix automigration error "Migration failed after first pass"
if ([[coordinator persistentStores] count] == 0)
{
- [coordinator performSelector:@selector(MR_addAutoMigratingSqliteStoreNamed:) withObject:storeFileName afterDelay:0.5];
+ [coordinator performSelector:@selector(MR_addAutoMigratingCustomStoreNamed:) withObject:@[storeFileName, storeType] afterDelay:0.5];
}
return coordinator;
@@ -271,6 +309,20 @@ + (NSPersistentStoreCoordinator *) MR_coordinatorWithPersistentStore:(NSPersiste
return psc;
}
++ (NSPersistentStoreCoordinator *) MR_coordinatorWithCustomStoreNamed:(NSString *)storeFileName storeType:(NSString*)storeType
+{
+ if (!storeType) {
+ MRLog(@"Loading default SQLite store type");
+ storeType = NSSQLiteStoreType;
+ }
+
+ NSManagedObjectModel *model = [NSManagedObjectModel MR_defaultManagedObjectModel];
+ NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
+
+ [psc MR_addCustomStoreNamed:storeFileName withOptions:nil storeType:storeType];
+ return psc;
+}
+
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithSqliteStoreNamed:(NSString *)storeFileName withOptions:(NSDictionary *)options
{
NSManagedObjectModel *model = [NSManagedObjectModel MR_defaultManagedObjectModel];
View
4 MagicalRecord/Core/MagicalRecord+Setup.h
@@ -13,9 +13,13 @@
+ (void) setupCoreDataStack;
+ (void) setupCoreDataStackWithInMemoryStore;
+ (void) setupAutoMigratingCoreDataStack;
++ (void) setupCoreDataStackWithType:(NSString*)storeType;
++ (void) setupAutoMigratingCoreDataStackWithType:(NSString*)storeType;
+ (void) setupCoreDataStackWithStoreNamed:(NSString *)storeName;
++ (void) setupCoreDataStackWithStoreNamed:(NSString *)storeName andCustomStoreType:(NSString*)storeType;
+ (void) setupCoreDataStackWithAutoMigratingSqliteStoreNamed:(NSString *)storeName;
++ (void) setupCoreDataStackWithAutoMigratingCustomStoreNamed:(NSString *)storeName andCustomStoreType:(NSString*)storeType;
@end
View
30 MagicalRecord/Core/MagicalRecord+Setup.m
@@ -23,26 +23,48 @@ + (void) setupAutoMigratingCoreDataStack
[self setupCoreDataStackWithAutoMigratingSqliteStoreNamed:[self defaultStoreName]];
}
-+ (void) setupCoreDataStackWithStoreNamed:(NSString *)storeName
++ (void) setupAutoMigratingCoreDataStackWithType:(NSString*)storeType
+{
+ [self setupCoreDataStackWithAutoMigratingCustomStoreNamed:[self defaultStoreName] andCustomStoreType:storeType];
+}
+
++ (void) setupCoreDataStackWithType:(NSString*)storeType
{
+ [self setupCoreDataStackWithStoreNamed:[self defaultStoreName] andCustomStoreType:storeType];
+}
+
++ (void) setupCoreDataStackWithStoreNamed:(NSString *)storeName andCustomStoreType:(NSString*)storeType {
if ([NSPersistentStoreCoordinator MR_defaultStoreCoordinator] != nil) return;
- NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_coordinatorWithSqliteStoreNamed:storeName];
+ NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_coordinatorWithCustomStoreNamed:storeName storeType:storeType];
[NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:coordinator];
[NSManagedObjectContext MR_initializeDefaultContextWithCoordinator:coordinator];
}
-+ (void) setupCoreDataStackWithAutoMigratingSqliteStoreNamed:(NSString *)storeName
++ (void) setupCoreDataStackWithStoreNamed:(NSString *)storeName
+{
+ [self setupCoreDataStackWithStoreNamed:storeName andCustomStoreType:nil];
+}
+
++ (void) setupCoreDataStackWithAutoMigratingCustomStoreNamed:(NSString *)storeName andCustomStoreType:(NSString*)storeType
{
if ([NSPersistentStoreCoordinator MR_defaultStoreCoordinator] != nil) return;
- NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_coordinatorWithAutoMigratingSqliteStoreNamed:storeName];
+ if (!storeName) {
+ storeName = [self defaultStoreName];
+ }
+ NSPersistentStoreCoordinator *coordinator = [NSPersistentStoreCoordinator MR_coordinatorWithAutoMigratingCustomStoreNamed:storeName storeType:storeType];
[NSPersistentStoreCoordinator MR_setDefaultStoreCoordinator:coordinator];
[NSManagedObjectContext MR_initializeDefaultContextWithCoordinator:coordinator];
}
++ (void) setupCoreDataStackWithAutoMigratingSqliteStoreNamed:(NSString *)storeName
+{
+ [self setupCoreDataStackWithAutoMigratingCustomStoreNamed:storeName andCustomStoreType:NSSQLiteStoreType];
+}
+
+ (void) setupCoreDataStackWithInMemoryStore;
{
if ([NSPersistentStoreCoordinator MR_defaultStoreCoordinator] != nil) return;

0 comments on commit b30bb2b

Please sign in to comment.
Something went wrong with that request. Please try again.