Permalink
Browse files

Added new nestedContextSave method with completion handler

  • Loading branch information...
1 parent 4091c78 commit 2660e73adf112ec0404fb28c0c9ff1ab08073709 @blackgold9 blackgold9 committed Oct 3, 2012
@@ -19,5 +19,6 @@
- (void) MR_saveNestedContexts;
- (void) MR_saveNestedContextsErrorHandler:(void (^)(NSError *))errorCallback;
+- (void) MR_saveNestedContextsErrorHandler:(void (^)(NSError *))errorCallback completion:(void (^)(void))completion;
@end
@@ -65,10 +65,25 @@ - (void) MR_saveNestedContexts;
- (void) MR_saveNestedContextsErrorHandler:(void (^)(NSError *))errorCallback;
{
- [self performBlockAndWait:^{
+ [self MR_saveNestedContextsErrorHandler:nil completion:nil];
+}
+
+- (void) MR_saveNestedContextsErrorHandler:(void (^)(NSError *))errorCallback completion:(void (^)(void))completion;
+{
+ [self performBlock:^{
[self MR_saveWithErrorCallback:errorCallback];
+ if (self.parentContext) {
+ [[self parentContext] performBlock:^{
+ [[self parentContext] MR_saveNestedContextsErrorHandler:errorCallback completion:completion];
+ }];
+ } else {
+ if (completion) {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ completion();
+ });
+ }
+ }
}];
- [[self parentContext] MR_saveNestedContextsErrorHandler:errorCallback];
}
- (void) MR_save;
@@ -120,5 +135,4 @@ - (void) MR_saveInBackgroundErrorHandler:(void (^)(NSError *))errorCallback comp
}
}];
}
-
@end
@@ -37,7 +37,7 @@ + (void) saveInBackgroundUsingContext:(NSManagedObjectContext *)localContext blo
dispatch_async(action_queue(), ^{
block(localContext);
- [localContext MR_saveInBackgroundErrorHandler:errorHandler completion:completion];
+ [localContext MR_saveNestedContextsErrorHandler:errorHandler completion:completion];
});
}
@@ -42,4 +42,27 @@ - (void)testBackgroundSaveCallsCompletionHandler
expect(didSave).will.beTruthy();
}
+- (void)testBackgroundSavesActuallySave
+{
+ __block NSManagedObjectID *objectId;
+ __block NSManagedObject *fetchedObject;
+ dispatch_group_t group = dispatch_group_create();
+ dispatch_group_enter(group);
+ [MagicalRecord saveInBackgroundWithBlock:^(NSManagedObjectContext *localContext) {
+ NSManagedObject *inserted = [SingleEntityWithNoRelationships MR_createInContext:localContext];
+ expect([inserted hasChanges]).to.beTruthy();
+ [localContext obtainPermanentIDsForObjects:@[inserted] error:nil];
+ objectId = inserted.objectID;
+
+ } completion:^{
+ fetchedObject = [[NSManagedObjectContext MR_rootSavingContext] objectWithID:objectId];
+ dispatch_group_leave(group);
+
+ }];
+
+ dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
+ expect(fetchedObject).toNot.beNil();
+ expect([fetchedObject hasChanges]).to.beFalsy();
+}
+
@end

0 comments on commit 2660e73

Please sign in to comment.