Execute save completion blocks on the main dispatch queue.

Addresses #351 in the context of saving.
This commit is contained in:
Tony Arnold
2013-01-02 00:53:34 +11:00
parent 357b62e323
commit 065352d80d
3 changed files with 29 additions and 14 deletions

View File

@@ -43,7 +43,9 @@
if (completion)
{
completion(NO, nil);
dispatch_async(dispatch_get_main_queue(), ^{
completion(NO, nil);
});
}
return;
@@ -72,7 +74,9 @@
[MagicalRecord handleErrors:error];
if (completion) {
completion(saved, error);
dispatch_async(dispatch_get_main_queue(), ^{
completion(saved, error);
});
}
} else {
// If we're the default context, save to disk too (the user expects it to persist)
@@ -86,8 +90,11 @@
// If we are not the default context (And therefore need to save the root context, do the completion action if one was specified
else {
MRLog(@"→ Finished saving: %@", [self MR_description]);
if (completion) {
completion(saved, error);
dispatch_async(dispatch_get_main_queue(), ^{
completion(saved, error);
});
}
}
}

View File

@@ -40,14 +40,16 @@ describe(@"MagicalRecord", ^{
});
context(@"asynchronous save action", ^{
it(@"should call completion block", ^{
it(@"should call completion block on the main thread", ^{
__block BOOL completionBlockCalled = NO;
__block BOOL completionBlockIsOnMainThread = NO;
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
[SingleEntityWithNoRelationships MR_createInContext:localContext];
} completion:^(BOOL success, NSError *error) {
// Ignore the success state we only care that this block is executed
// Ignore the success state we only care that this block is executed on the main thread
completionBlockCalled = YES;
completionBlockIsOnMainThread = [NSThread isMainThread];
}];
[[expectFutureValue(@(completionBlockCalled)) shouldEventually] beTrue];
@@ -100,14 +102,16 @@ describe(@"MagicalRecord", ^{
});
context(@"running asynchronously", ^{
it(@"should call completion block", ^{
it(@"should call completion block on the main thread", ^{
__block BOOL completionBlockCalled = NO;
__block BOOL completionBlockIsOnMainThread = NO;
[MagicalRecord saveUsingCurrentThreadContextWithBlock:^(NSManagedObjectContext *localContext) {
[SingleEntityWithNoRelationships MR_createInContext:localContext];
} completion:^(BOOL success, NSError *error) {
// Ignore the success state we only care that this block is executed
// Ignore the success state we only care that this block is executed on the main thread
completionBlockCalled = YES;
completionBlockIsOnMainThread = [NSThread isMainThread];
}];
[[expectFutureValue(@(completionBlockCalled)) shouldEventually] beTrue];

View File

@@ -53,13 +53,15 @@ describe(@"NSManagedObjectContext+MagicalSaves", ^{
});
context(@"asynchronously", ^{
it(@"should call completion block", ^{
it(@"and should call the completion block on the main thread", ^{
__block BOOL completionBlockCalled = NO;
__block BOOL completionBlockIsOnMainThread = NO;
[SingleEntityWithNoRelationships MR_createInContext:managedObjectContext];
[managedObjectContext MR_saveOnlySelfWithCompletion:^(BOOL success, NSError *error) {
completionBlockCalled = YES;
completionBlockIsOnMainThread = [NSThread isMainThread];
}];
[[expectFutureValue(@(completionBlockCalled)) shouldEventually] beTrue];
@@ -123,15 +125,17 @@ describe(@"NSManagedObjectContext+MagicalSaves", ^{
});
context(@"asynchronously", ^{
it(@"and should call completion block", ^{
it(@"and should call the completion block on the main thread", ^{
__block BOOL completionBlockCalled = NO;
__block BOOL completionBlockIsOnMainThread = NO;
[SingleEntityWithNoRelationships MR_createInContext:managedObjectContext];
[[@([managedObjectContext hasChanges]) should] beTrue];
[managedObjectContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
completionBlockCalled = YES;
completionBlockIsOnMainThread = [NSThread isMainThread];
}];
[[expectFutureValue(@(completionBlockCalled)) shouldEventually] beTrue];
@@ -290,7 +294,7 @@ describe(@"NSManagedObjectContext+MagicalSaves", ^{
errorHandlerCalled = YES;
}];
[[@(errorHandlerCalled) should] beTrue];
[[expectFutureValue(@(errorHandlerCalled)) shouldEventually] beTrue];
});
it(@"should save", ^{
@@ -441,7 +445,7 @@ describe(@"NSManagedObjectContext+MagicalSaves", ^{
errorHandlerCalled = YES;
}];
[[@(errorHandlerCalled) should] beTrue];
[[expectFutureValue(@(errorHandlerCalled)) shouldEventually] beTrue];
});
it(@"should save", ^{