Merge pull request #288 from kirsplatrick/experimental-dispatchgroup

Update to support batch completion block firing after all dependent completion blocks.
This commit is contained in:
Mattt Thompson 2012-04-12 19:31:52 -07:00
commit 23763688de
7 changed files with 89 additions and 29 deletions

View file

@ -515,24 +515,17 @@ static void AFReachabilityCallback(SCNetworkReachabilityRef __unused target, SCN
progressBlock:(void (^)(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations))progressBlock progressBlock:(void (^)(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations))progressBlock
completionBlock:(void (^)(NSArray *operations))completionBlock completionBlock:(void (^)(NSArray *operations))completionBlock
{ {
NSBlockOperation *batchedOperation = [NSBlockOperation blockOperationWithBlock:^{ dispatch_group_t dispatchGroup = dispatch_group_create();
if (completionBlock) {
dispatch_async(dispatch_get_main_queue(), ^{
completionBlock(operations);
});
}
}];
[self.operationQueue addOperation:batchedOperation];
NSPredicate *finishedOperationPredicate = [NSPredicate predicateWithFormat:@"isFinished == YES"]; NSPredicate *finishedOperationPredicate = [NSPredicate predicateWithFormat:@"isFinished == YES"];
for (AFHTTPRequestOperation *operation in operations) { for (AFHTTPRequestOperation *operation in operations) {
AFCompletionBlock originalCompletionBlock = [[operation.completionBlock copy] autorelease]; AFCompletionBlock originalCompletionBlock = [[operation.completionBlock copy] autorelease];
operation.dispatchGroup = dispatchGroup;
operation.completionBlock = ^{ operation.completionBlock = ^{
if (progressBlock) { if (progressBlock) {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_group_async(dispatchGroup, dispatch_get_main_queue(), ^{
progressBlock([[batchedOperation.dependencies filteredArrayUsingPredicate:finishedOperationPredicate] count], [batchedOperation.dependencies count]); progressBlock([[operations filteredArrayUsingPredicate:finishedOperationPredicate] count], [operations count]);
}); });
} }
@ -540,10 +533,16 @@ static void AFReachabilityCallback(SCNetworkReachabilityRef __unused target, SCN
originalCompletionBlock(); originalCompletionBlock();
} }
}; };
[batchedOperation addDependency:operation];
[self enqueueHTTPRequestOperation:operation]; [self enqueueHTTPRequestOperation:operation];
} }
if (completionBlock) {
dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^{
completionBlock(operations);
});
}
dispatch_release(dispatchGroup);
} }
#pragma mark - #pragma mark -

View file

@ -76,6 +76,10 @@
*/ */
@property (nonatomic) dispatch_queue_t failureCallbackQueue; @property (nonatomic) dispatch_queue_t failureCallbackQueue;
/**
The dispatch group on which to call the completion/failure block
*/
@property (nonatomic) dispatch_group_t dispatchGroup;
/** /**
A Boolean value determining whether or not the class can process the specified request. For example, `AFJSONRequestOperation` may check to make sure the content type was `application/json` or the URL path extension was `.json`. A Boolean value determining whether or not the class can process the specified request. For example, `AFJSONRequestOperation` may check to make sure the content type was `application/json` or the URL path extension was `.json`.

View file

@ -56,6 +56,8 @@ static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) {
@interface AFHTTPRequestOperation () @interface AFHTTPRequestOperation ()
@property (readwrite, nonatomic, retain) NSError *HTTPError; @property (readwrite, nonatomic, retain) NSError *HTTPError;
@property (nonatomic) dispatch_once_t onceToken;
@property (atomic) dispatch_semaphore_t dispatchSemaphore;
@end @end
@implementation AFHTTPRequestOperation @implementation AFHTTPRequestOperation
@ -64,6 +66,9 @@ static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) {
@synthesize HTTPError = _HTTPError; @synthesize HTTPError = _HTTPError;
@synthesize successCallbackQueue = _successCallbackQueue; @synthesize successCallbackQueue = _successCallbackQueue;
@synthesize failureCallbackQueue = _failureCallbackQueue; @synthesize failureCallbackQueue = _failureCallbackQueue;
@synthesize dispatchGroup = _dispatchGroup;
@synthesize onceToken = _onceToken;
@synthesize dispatchSemaphore = _dispatchSemaphore;
- (id)initWithRequest:(NSURLRequest *)request { - (id)initWithRequest:(NSURLRequest *)request {
@ -73,6 +78,8 @@ static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) {
} }
self.acceptableStatusCodes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)]; self.acceptableStatusCodes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(200, 100)];
self.dispatchSemaphore = dispatch_semaphore_create(1);
self.completionBlock = NULL;
return self; return self;
} }
@ -92,6 +99,16 @@ static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) {
_failureCallbackQueue = NULL; _failureCallbackQueue = NULL;
} }
if (_dispatchGroup) {
dispatch_release(_dispatchGroup);
_dispatchGroup = NULL;
}
if (_dispatchSemaphore) {
dispatch_release(_dispatchSemaphore);
_dispatchSemaphore = NULL;
}
[super dealloc]; [super dealloc];
} }
@ -157,6 +174,46 @@ static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) {
} }
} }
- (void)setDispatchGroup:(dispatch_group_t)dispatchGroup {
dispatch_semaphore_wait(self.dispatchSemaphore, DISPATCH_TIME_FOREVER);
if (dispatchGroup != _dispatchGroup) {
if (_dispatchGroup) {
dispatch_group_leave(_dispatchGroup);
dispatch_release(_dispatchGroup);
_dispatchGroup = NULL;
}
if (dispatchGroup) {
dispatch_retain(dispatchGroup);
_dispatchGroup = dispatchGroup;
dispatch_group_enter(_dispatchGroup);
}
}
dispatch_semaphore_signal(self.dispatchSemaphore);
}
- (dispatch_group_t)dispatchGroup {
dispatch_semaphore_wait(self.dispatchSemaphore, DISPATCH_TIME_FOREVER);
if(_dispatchGroup == NULL) {
_dispatchGroup = dispatch_group_create();
dispatch_group_enter(_dispatchGroup);
}
dispatch_semaphore_signal(self.dispatchSemaphore);
return _dispatchGroup;
}
- (void)setCompletionBlock:(void (^)(void))block {
[super setCompletionBlock:^{
if(block) {
block();
}
// Dispatch once is used to ensure that setting the block with this block will not cause multiple calls to 'dispatch_group_leave'
dispatch_once(&_onceToken, ^{
dispatch_group_leave(self.dispatchGroup);
});
}];
}
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success - (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
{ {
@ -167,13 +224,13 @@ static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) {
if (self.error) { if (self.error) {
if (failure) { if (failure) {
dispatch_async(self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{ dispatch_group_async(self.dispatchGroup, self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{
failure(self, self.error); failure(self, self.error);
}); });
} }
} else { } else {
if (success) { if (success) {
dispatch_async(self.successCallbackQueue ? self.successCallbackQueue : dispatch_get_main_queue(), ^{ dispatch_group_async(self.dispatchGroup, self.successCallbackQueue ? self.successCallbackQueue : dispatch_get_main_queue(), ^{
success(self, self.responseData); success(self, self.responseData);
}); });
} }

View file

@ -226,10 +226,10 @@ static dispatch_queue_t image_request_operation_processing_queue() {
return; return;
} }
dispatch_async(image_request_operation_processing_queue(), ^(void) { dispatch_group_async(self.dispatchGroup, image_request_operation_processing_queue(), ^(void) {
if (self.error) { if (self.error) {
if (failure) { if (failure) {
dispatch_async(self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{ dispatch_group_async(self.dispatchGroup, self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{
failure(self, self.error); failure(self, self.error);
}); });
} }
@ -243,7 +243,7 @@ static dispatch_queue_t image_request_operation_processing_queue() {
image = self.responseImage; image = self.responseImage;
dispatch_async(self.successCallbackQueue ? self.successCallbackQueue : dispatch_get_main_queue(), ^{ dispatch_group_async(self.dispatchGroup, self.successCallbackQueue ? self.successCallbackQueue : dispatch_get_main_queue(), ^{
success(self, image); success(self, image);
}); });
} }

View file

@ -125,23 +125,23 @@ static dispatch_queue_t json_request_operation_processing_queue() {
if (self.error) { if (self.error) {
if (failure) { if (failure) {
dispatch_async(self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{ dispatch_group_async(self.dispatchGroup, self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{
failure(self, self.error); failure(self, self.error);
}); });
} }
} else { } else {
dispatch_async(json_request_operation_processing_queue(), ^(void) { dispatch_group_async(self.dispatchGroup, json_request_operation_processing_queue(), ^(void) {
id JSON = self.responseJSON; id JSON = self.responseJSON;
if (self.JSONError) { if (self.JSONError) {
if (failure) { if (failure) {
dispatch_async(self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{ dispatch_group_async(self.dispatchGroup, self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{
failure(self, self.error); failure(self, self.error);
}); });
} }
} else { } else {
if (success) { if (success) {
dispatch_async(self.successCallbackQueue ? self.successCallbackQueue : dispatch_get_main_queue(), ^{ dispatch_group_async(self.dispatchGroup, self.successCallbackQueue ? self.successCallbackQueue : dispatch_get_main_queue(), ^{
success(self, JSON); success(self, JSON);
}); });
} }

View file

@ -125,23 +125,23 @@ static dispatch_queue_t property_list_request_operation_processing_queue() {
if (self.error) { if (self.error) {
if (failure) { if (failure) {
dispatch_async(self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{ dispatch_group_async(self.dispatchGroup, self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{
failure(self, self.error); failure(self, self.error);
}); });
} }
} else { } else {
dispatch_async(property_list_request_operation_processing_queue(), ^(void) { dispatch_group_async(self.dispatchGroup, property_list_request_operation_processing_queue(), ^(void) {
id propertyList = self.responsePropertyList; id propertyList = self.responsePropertyList;
if (self.propertyListError) { if (self.propertyListError) {
if (failure) { if (failure) {
dispatch_async(self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{ dispatch_group_async(self.dispatchGroup, self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{
failure(self, self.error); failure(self, self.error);
}); });
} }
} else { } else {
if (success) { if (success) {
dispatch_async(self.successCallbackQueue ? self.successCallbackQueue : dispatch_get_main_queue(), ^{ dispatch_group_async(self.dispatchGroup, self.successCallbackQueue ? self.successCallbackQueue : dispatch_get_main_queue(), ^{
success(self, propertyList); success(self, propertyList);
}); });
} }

View file

@ -170,18 +170,18 @@ static dispatch_queue_t xml_request_operation_processing_queue() {
return; return;
} }
dispatch_async(xml_request_operation_processing_queue(), ^(void) { dispatch_group_async(self.dispatchGroup, xml_request_operation_processing_queue(), ^(void) {
NSXMLParser *XMLParser = self.responseXMLParser; NSXMLParser *XMLParser = self.responseXMLParser;
if (self.error) { if (self.error) {
if (failure) { if (failure) {
dispatch_async(self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{ dispatch_group_async(self.dispatchGroup, self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{
failure(self, self.error); failure(self, self.error);
}); });
} }
} else { } else {
if (success) { if (success) {
dispatch_async(self.successCallbackQueue ? self.successCallbackQueue : dispatch_get_main_queue(), ^{ dispatch_group_async(self.dispatchGroup, self.successCallbackQueue ? self.successCallbackQueue : dispatch_get_main_queue(), ^{
success(self, XMLParser); success(self, XMLParser);
}); });
} }