Merge branch 'custom-callback-queues' of https://github.com/steipete/AFNetworking into steipete-custom-callback-queues

This commit is contained in:
Mattt Thompson 2012-02-20 19:42:36 -08:00
commit 64555e5dff
6 changed files with 100 additions and 63 deletions

View file

@ -31,6 +31,8 @@
NSIndexSet *_acceptableStatusCodes; NSIndexSet *_acceptableStatusCodes;
NSSet *_acceptableContentTypes; NSSet *_acceptableContentTypes;
NSError *_HTTPError; NSError *_HTTPError;
dispatch_queue_t _successCallbackQueue;
dispatch_queue_t _failureCallbackQueue;
} }
///---------------------------------------------- ///----------------------------------------------
@ -71,6 +73,17 @@
*/ */
@property (readonly) BOOL hasAcceptableContentType; @property (readonly) BOOL hasAcceptableContentType;
/**
The callback dispatch queue on success. If this is NULL (default), the main queue is used.
*/
@property (nonatomic) dispatch_queue_t successCallbackQueue;
/**
The callback dispatch queue on failure. If this is NULL (default), the main queue is used.
*/
@property (nonatomic) dispatch_queue_t failureCallbackQueue;
/** /**
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`.
@ -94,3 +107,18 @@
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;
@end @end
@interface AFHTTPRequestOperation (AFInternal)
/**
Executes the successBlock on the corresponding successCallbackQueue.
*/
- (void)dispatchSuccessBlock:(void (^)(AFHTTPRequestOperation *operation, id responseObject))successBlock responseObject:(id)responseObject;
/**
Executes the failureBlock on the corresponding failureCallbackQueue.
*/
- (void)dispatchFailureBlock:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failureBlock;
@end

View file

@ -30,6 +30,9 @@
@synthesize acceptableStatusCodes = _acceptableStatusCodes; @synthesize acceptableStatusCodes = _acceptableStatusCodes;
@synthesize acceptableContentTypes = _acceptableContentTypes; @synthesize acceptableContentTypes = _acceptableContentTypes;
@synthesize HTTPError = _HTTPError; @synthesize HTTPError = _HTTPError;
@synthesize successCallbackQueue = _successCallbackQueue;
@synthesize failureCallbackQueue = _failureCallbackQueue;
- (id)initWithRequest:(NSURLRequest *)request { - (id)initWithRequest:(NSURLRequest *)request {
self = [super initWithRequest:request]; self = [super initWithRequest:request];
@ -46,6 +49,8 @@
[_acceptableStatusCodes release]; [_acceptableStatusCodes release];
[_acceptableContentTypes release]; [_acceptableContentTypes release];
[_HTTPError release]; [_HTTPError release];
if (_successCallbackQueue) { dispatch_release(_successCallbackQueue), _successCallbackQueue=NULL;}
if (_failureCallbackQueue) { dispatch_release(_failureCallbackQueue), _failureCallbackQueue=NULL;}
[super dealloc]; [super dealloc];
} }
@ -85,6 +90,34 @@
return !self.acceptableContentTypes || [self.acceptableContentTypes containsObject:[self.response MIMEType]]; return !self.acceptableContentTypes || [self.acceptableContentTypes containsObject:[self.response MIMEType]];
} }
- (void)setSuccessCallbackQueue:(dispatch_queue_t)successCallbackQueue {
if (successCallbackQueue != _successCallbackQueue) {
if (_successCallbackQueue) {
dispatch_release(_successCallbackQueue);
}
if (successCallbackQueue) {
dispatch_retain(successCallbackQueue);
_successCallbackQueue = successCallbackQueue;
}
}
}
- (void)setFailureCallbackQueue:(dispatch_queue_t)failureCallbackQueue {
if (failureCallbackQueue != _failureCallbackQueue) {
if (_failureCallbackQueue) {
dispatch_release(_failureCallbackQueue);
}
if (failureCallbackQueue) {
dispatch_retain(failureCallbackQueue);
_failureCallbackQueue = failureCallbackQueue;
}
}
}
- (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
{ {
@ -94,17 +127,9 @@
} }
if (self.error) { if (self.error) {
if (failure) { [self dispatchFailureBlock:failure];
dispatch_async(dispatch_get_main_queue(), ^(void) {
failure(self, self.error);
});
}
} else { } else {
if (success) { [self dispatchSuccessBlock:success responseObject:self.responseString];
dispatch_async(dispatch_get_main_queue(), ^{
success(self, self.responseData);
});
}
} }
}; };
} }
@ -113,6 +138,24 @@
+ (BOOL)canProcessRequest:(NSURLRequest *)request { + (BOOL)canProcessRequest:(NSURLRequest *)request {
return YES; return YES;
} }
#pragma mark - AFInternal
- (void)dispatchSuccessBlock:(void (^)(AFHTTPRequestOperation *operation, id responseObject))successBlock responseObject:(id)responseObject {
if (successBlock) {
dispatch_async(self.successCallbackQueue ? self.successCallbackQueue : dispatch_get_main_queue(), ^{
successBlock(self, responseObject);
});
}
}
- (void)dispatchFailureBlock:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failureBlock {
if (failureBlock) {
dispatch_async(self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{
failureBlock(self, self.error);
});
}
}
@end @end

View file

@ -230,17 +230,9 @@ static dispatch_queue_t image_request_operation_processing_queue() {
dispatch_async(image_request_operation_processing_queue(), ^(void) { dispatch_async(image_request_operation_processing_queue(), ^(void) {
if (self.error) { if (self.error) {
if (failure) { [self dispatchFailureBlock:failure];
dispatch_async(dispatch_get_main_queue(), ^(void) { } else {
failure(self, self.error); [self dispatchSuccessBlock:success responseObject:self.responseImage];
});
}
} else {
if (success) {
dispatch_async(dispatch_get_main_queue(), ^(void) {
success(self, self.responseImage);
});
}
} }
}); });
}; };

View file

@ -124,26 +124,16 @@ static dispatch_queue_t json_request_operation_processing_queue() {
} }
if (self.error) { if (self.error) {
if (failure) { [self dispatchFailureBlock:failure];
dispatch_async(dispatch_get_main_queue(), ^(void) {
failure(self, self.error);
});
}
} else { } else {
dispatch_async(json_request_operation_processing_queue(), ^(void) { dispatch_async(json_request_operation_processing_queue(), ^(void) {
id JSON = self.responseJSON; id JSON = self.responseJSON;
dispatch_async(dispatch_get_main_queue(), ^(void) { if (self.JSONError) {
if (self.JSONError) { [self dispatchFailureBlock:failure];
if (failure) { } else {
failure(self, self.JSONError); [self dispatchSuccessBlock:success responseObject:JSON];
} }
} else {
if (success) {
success(self, JSON);
}
}
});
}); });
} }
}; };

View file

@ -124,26 +124,16 @@ static dispatch_queue_t property_list_request_operation_processing_queue() {
} }
if (self.error) { if (self.error) {
if (failure) { [self dispatchFailureBlock:failure];
dispatch_async(dispatch_get_main_queue(), ^(void) {
failure(self, self.error);
});
}
} else { } else {
dispatch_async(property_list_request_operation_processing_queue(), ^(void) { dispatch_async(property_list_request_operation_processing_queue(), ^(void) {
id propertyList = self.responsePropertyList; id propertyList = self.responsePropertyList;
dispatch_async(dispatch_get_main_queue(), ^(void) { if (self.propertyListError) {
if (self.propertyListError) { [self dispatchFailureBlock:failure];
if (failure) { }else {
failure(self, self.propertyListError); [self dispatchSuccessBlock:success responseObject:propertyList];
} }
} else {
if (success) {
success(self, propertyList);
}
}
});
}); });
} }
}; };

View file

@ -187,15 +187,9 @@ static dispatch_queue_t xml_request_operation_processing_queue() {
} }
if (self.error) { if (self.error) {
if (failure) { [self dispatchFailureBlock:failure];
dispatch_async(dispatch_get_main_queue(), ^(void) {
failure(self, self.error);
});
}
} else { } else {
if (success) { [self dispatchSuccessBlock:success responseObject:self.responseXMLParser];
success(self, self.responseXMLParser);
}
} }
}; };
} }