Fixes #1453. Completion block for group of requests should be called after all completion blocks have finished executing.

This commit is contained in:
Denys Telezhkin 2013-10-08 00:25:10 +03:00
parent a4b6ed34e3
commit 731f033bc9
2 changed files with 42 additions and 1 deletions

View File

@ -68,6 +68,10 @@ static dispatch_group_t http_request_operation_completion_group() {
#pragma clang diagnostic ignored "-Warc-retain-cycles"
#pragma clang diagnostic ignored "-Wgnu"
self.completionBlock = ^{
if (self.completionGroup)
{
dispatch_group_enter(self.completionGroup);
}
dispatch_async(http_request_operation_processing_queue(), ^{
if (self.error) {
if (failure) {
@ -94,7 +98,11 @@ static dispatch_group_t http_request_operation_completion_group() {
});
}
}
}
}
if (self.completionGroup)
{
dispatch_group_leave(self.completionGroup);
}
});
};
#pragma clang diagnostic pop

View File

@ -275,4 +275,37 @@
[[NSNotificationCenter defaultCenter] removeObserver:observer];
}
-(void)testThatCompletionBlockForBatchRequestsIsFiredAfterAllOperationCompletionBlocks
{
__block BOOL firstBlock = NO;
__block BOOL secondBlock = NO;
NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/get" relativeToURL:self.baseURL]];
AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:request1];
[operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
firstBlock = YES;
} failure:nil];
[operation1 setResponseSerializer:[AFHTTPResponseSerializer serializer]];
NSURLRequest *request2 = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/1" relativeToURL:self.baseURL]];
AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:request2];
[operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
secondBlock = YES;
} failure:nil];
[operation2 setResponseSerializer:[AFHTTPResponseSerializer serializer]];
__block BOOL completionBlockFiredAfterOtherBlocks = NO;
NSArray * batchRequests = [AFURLConnectionOperation batchOfRequestOperations:@[operation1,operation2]
progressBlock:nil
completionBlock:^(NSArray *operations) {
if (firstBlock && secondBlock)
{
completionBlockFiredAfterOtherBlocks = YES;
}
}];
NSOperationQueue * queue = [NSOperationQueue new];
[queue addOperations:batchRequests waitUntilFinished:NO];
expect(completionBlockFiredAfterOtherBlocks).will.beTruthy();
}
@end