Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Persist changes to disk when using:

- [NSManagedObjectContext MR_saveInBackgroundErrorHandler:completion:] methods, AND the context is the default context
- [MagicalRecord saveInBackground…] methods
  • Loading branch information...
commit 8bb0d0de7428c376b6561b289eca7fb1a261d05a 1 parent f696791
@casademora casademora authored
2  .gitignore
@@ -19,3 +19,5 @@ UserInterfaceState.xcuserstate
/Magical Record.xcodeproj/xcuserdata/saul.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
/Magical Record.xcodeproj/xcuserdata/gfurman.xcuserdatad
Project Files/Magical Record.xcodeproj/xcuserdata
+Project Files/MagicalRecord.xcodeproj/xcuserdata/saul.xcuserdatad/xcdebugger/Breakpoints.xcbkptlist
+Project Files/MagicalRecord.xcodeproj/xcuserdata/saul.xcuserdatad/xcschemes/iOS Test Runner.xcscheme
19 MagicalRecord/Categories/NSManagedObjectContext/NSManagedObjectContext+MagicalSaves.m
@@ -91,21 +91,26 @@ - (void) MR_saveInBackgroundCompletion:(void (^)(void))completion;
[self MR_saveInBackgroundErrorHandler:nil completion:completion];
+- (void) MR_saveInBackgroundErrorHandler:(void (^)(NSError *))errorCallback;
+ [self MR_saveInBackgroundErrorHandler:errorCallback completion:nil];
- (void) MR_saveInBackgroundErrorHandler:(void (^)(NSError *))errorCallback completion:(void (^)(void))completion;
[self performBlock:^{
[self MR_saveWithErrorCallback:errorCallback];
- if (completion)

Hi Saul, are you sure this is correct. This new version now only runs the completion block only if it is the MR_defaultContext or MR_rootSavingContext. Shouldn't this run the completion block (if it is not nil) regardless of which context we're dealing with?

YDing added a note

Switching this back to if (completion) solved my problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ if (self == [[self class] MR_defaultContext])
+ {
+ [[[self class] MR_rootSavingContext] MR_saveInBackgroundErrorHandler:errorCallback completion:completion];
+ }
+ if (self == [[self class] MR_rootSavingContext])
dispatch_async(dispatch_get_main_queue(), completion);
-- (void) MR_saveInBackgroundErrorHandler:(void (^)(NSError *))errorCallback;
- [self MR_saveInBackgroundErrorHandler:errorCallback completion:nil];
4 MagicalRecord/Core/MagicalRecord+Actions.m
@@ -66,12 +66,12 @@ + (void) saveWithBlock:(void (^)(NSManagedObjectContext *localContext))block com
if ([localContext hasChanges])
- [localContext MR_saveErrorHandler:errorHandler];
+ [localContext MR_saveNestedContextsErrorHandler:errorHandler];
if (completion)
- completion();
+ dispatch_async(dispatch_get_main_queue(), completion);

3 comments on commit 8bb0d0d


I'm seeing my data only save now if it uses saveDataWithBlock. saveInBackgroundWithBlock does not persist.


I'm seeing that when calling saveInBackgroundWithBlock: completion: doesn't actually call the completion. I have some code that looks like this, and that last NSLog never gets printed.:

if ([message isMGChatMessageWithBody]) { //not a message!

    [MagicalRecord saveInBackgroundWithBlock:^(NSManagedObjectContext *context) {

        MyObject *messageObject = [MyObject MR_findFirstByAttribute:@"myobject_id" withValue:object_id inContext:context];

        if (messageObject == nil) {
            messageObject = [MyObject MR_createInContext:context];

        //set some properties on the object

    } completion:^{
        NSLog(@"SENDING NOTIFICATION!!!!11!!");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"savedone" object:self userInfo:nil];


Same as @nicklocking, only saveWithBlock is working for me.

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