Added methods to simplify calling the success/failure handlers.

This commit is contained in:
Peter Steinberger 2012-02-06 13:43:43 -08:00
parent feaf0814c4
commit 30a5cea4fc
6 changed files with 51 additions and 59 deletions

View file

@ -107,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 error:(NSError *)error;
@end

View file

@ -127,17 +127,9 @@
} }
if (self.error) { if (self.error) {
if (failure) { [self dispatchFailureBlock:failure error:self.error];
dispatch_async(self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{
failure(self, self.error);
});
}
} else { } else {
if (success) { [self dispatchSuccessBlock:success responseObject:self.responseString];
dispatch_async(self.successCallbackQueue ? self.successCallbackQueue : dispatch_get_main_queue(), ^{
success(self, self.responseData);
});
}
} }
}; };
} }
@ -148,4 +140,23 @@
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 error:(NSError *)error {
if (failureBlock) {
dispatch_async(self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{
failureBlock(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 error:self.error];
dispatch_async(self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{
failure(self, self.error);
});
}
} else { } else {
if (success) { [self dispatchSuccessBlock:success responseObject:self.responseImage];
dispatch_async(self.successCallbackQueue ? self.successCallbackQueue : dispatch_get_main_queue(), ^{
success(self, self.responseImage);
});
}
} }
}); });
}; };

View file

@ -134,17 +134,9 @@ static dispatch_queue_t json_request_operation_processing_queue() {
id JSON = self.responseJSON; id JSON = self.responseJSON;
if (self.JSONError) { if (self.JSONError) {
if (failure) { [self dispatchFailureBlock:failure error:self.JSONError];
dispatch_async(self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{
failure(self, self.JSONError);
});
}
} else { } else {
if (success) { [self dispatchSuccessBlock:success responseObject:JSON];
dispatch_async(self.successCallbackQueue ? self.successCallbackQueue : dispatch_get_main_queue(), ^{
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 error:self.error];
dispatch_async(self.failureCallbackQueue ? self.failureCallbackQueue : dispatch_get_main_queue(), ^{
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(self.successCallbackQueue ? self.successCallbackQueue : dispatch_get_main_queue(), ^{
if (self.propertyListError) { if (self.propertyListError) {
if (failure) { [self dispatchFailureBlock:failure error:self.propertyListError];
failure(self, self.propertyListError); }else {
[self dispatchSuccessBlock:success responseObject:propertyList];
} }
} else {
if (success) {
success(self, propertyList);
}
}
});
}); });
} }
}; };

View file

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