Refactoring AFImageRequestOperation to use setCompletionBlockWithSuccess:failure
This commit is contained in:
parent
f28cf63a96
commit
5b5b32c5b8
1 changed files with 59 additions and 66 deletions
|
|
@ -79,41 +79,25 @@ static dispatch_queue_t image_request_operation_processing_queue() {
|
|||
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
|
||||
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure
|
||||
{
|
||||
AFImageRequestOperation *operation = [[[AFImageRequestOperation alloc] initWithRequest:urlRequest] autorelease];
|
||||
|
||||
operation.completionBlock = ^ {
|
||||
if ([operation isCancelled]) {
|
||||
return;
|
||||
}
|
||||
|
||||
dispatch_async(image_request_operation_processing_queue(), ^(void) {
|
||||
if (operation.error) {
|
||||
if (failure) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
failure(operation.request, operation.response, operation.error);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
UIImage *image = operation.responseImage;
|
||||
AFImageRequestOperation *requestOperation = [[[AFImageRequestOperation alloc] initWithRequest:urlRequest] autorelease];
|
||||
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
|
||||
if (success) {
|
||||
UIImage *image = responseObject;
|
||||
|
||||
if (imageProcessingBlock) {
|
||||
image = imageProcessingBlock(image);
|
||||
}
|
||||
|
||||
if (success) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
success(operation.request, operation.response, image);
|
||||
});
|
||||
}
|
||||
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
||||
if (failure) {
|
||||
failure(operation.request, operation.response, error);
|
||||
}
|
||||
}];
|
||||
|
||||
if ([operation.request cachePolicy] != NSURLCacheStorageNotAllowed) {
|
||||
[[AFImageCache sharedImageCache] cacheImageData:operation.responseData forURL:[operation.request URL] cacheName:cacheNameOrNil];
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return operation;
|
||||
return requestOperation;
|
||||
}
|
||||
#elif __MAC_OS_X_VERSION_MIN_REQUIRED
|
||||
+ (AFImageRequestOperation *)imageRequestOperationWithRequest:(NSURLRequest *)urlRequest
|
||||
|
|
@ -122,41 +106,24 @@ static dispatch_queue_t image_request_operation_processing_queue() {
|
|||
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSImage *image))success
|
||||
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure
|
||||
{
|
||||
AFImageRequestOperation *operation = [[[AFImageRequestOperation alloc] initWithRequest:urlRequest] autorelease];
|
||||
|
||||
operation.completionBlock = ^ {
|
||||
if ([operation isCancelled]) {
|
||||
return;
|
||||
}
|
||||
|
||||
dispatch_async(image_request_operation_processing_queue(), ^(void) {
|
||||
if (operation.error) {
|
||||
if (failure) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
failure(operation.request, operation.response, operation.error);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
NSImage *image = operation.responseImage;
|
||||
AFImageRequestOperation *requestOperation = [[[AFImageRequestOperation alloc] initWithRequest:urlRequest] autorelease];
|
||||
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
|
||||
if (success) {
|
||||
NSImage *image = responseObject;
|
||||
|
||||
if (imageProcessingBlock) {
|
||||
image = imageProcessingBlock(image);
|
||||
}
|
||||
|
||||
if (success) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
success(operation.request, operation.response, image);
|
||||
});
|
||||
}
|
||||
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
||||
if (failure) {
|
||||
failure(operation.request, operation.response, error);
|
||||
}
|
||||
}];
|
||||
|
||||
if ([operation.request cachePolicy] != NSURLCacheStorageNotAllowed) {
|
||||
[[AFImageCache sharedImageCache] cacheImageData:operation.responseData forURL:[operation.request URL] cacheName:cacheNameOrNil];
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return operation;
|
||||
return requestOperation;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
@ -253,4 +220,30 @@ static dispatch_queue_t image_request_operation_processing_queue() {
|
|||
}
|
||||
#endif
|
||||
|
||||
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
||||
{
|
||||
self.completionBlock = ^ {
|
||||
if ([self isCancelled]) {
|
||||
return;
|
||||
}
|
||||
|
||||
dispatch_async(image_request_operation_processing_queue(), ^(void) {
|
||||
if (self.error) {
|
||||
if (failure) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
failure(self, self.error);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
if (success) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^(void) {
|
||||
success(self, self.responseImage);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue