Skip to content

Commit

Permalink
Creating custom GCD queues for JSON and image request processing
Browse files Browse the repository at this point in the history
  • Loading branch information
mattt committed Aug 15, 2011
1 parent a6ad381 commit da1fa38
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
11 changes: 10 additions & 1 deletion AFNetworking/AFImageRequestOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ static inline CGSize kAFImageRequestRoundedCornerRadii(CGSize imageSize) {
return CGSizeMake(dimension, dimension);
}

static dispatch_queue_t af_image_request_operation_processing_queue;
static dispatch_queue_t image_request_operation_processing_queue() {
if (af_image_request_operation_processing_queue == NULL) {
af_image_request_operation_processing_queue = dispatch_queue_create("com.alamofire.image-request.processing", 0);
}

return af_image_request_operation_processing_queue;
}

@implementation AFImageRequestOperation

+ (id)operationWithRequest:(NSURLRequest *)urlRequest
Expand All @@ -47,7 +56,7 @@ + (id)operationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(UIImage *image))success
{
return [self operationWithRequest:urlRequest completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
dispatch_async(image_request_operation_processing_queue(), ^(void) {
UIImage *image = nil;
if ([[UIScreen mainScreen] scale] == 2.0) {
CGImageRef imageRef = [[UIImage imageWithData:data] CGImage];
Expand Down
23 changes: 16 additions & 7 deletions AFNetworking/AFJSONRequestOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@

#include <Availability.h>

static dispatch_queue_t af_json_request_operation_processing_queue;
static dispatch_queue_t json_request_operation_processing_queue() {
if (af_json_request_operation_processing_queue == NULL) {
af_json_request_operation_processing_queue = dispatch_queue_create("com.alamofire.json-request.processing", 0);
}

return af_json_request_operation_processing_queue;
}

@implementation AFJSONRequestOperation

+ (id)operationWithRequest:(NSURLRequest *)urlRequest
Expand Down Expand Up @@ -62,23 +71,23 @@ + (id)operationWithRequest:(NSURLRequest *)urlRequest
failure(error);
}
} else {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
dispatch_async(json_request_operation_processing_queue(), ^(void) {
id JSON = nil;

NSError *JSONError = nil;
#if __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_4_3
if ([NSJSONSerialization class]) {
JSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
JSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&JSONError];
} else {
JSON = [[JSONDecoder decoder] objectWithData:data error:&error];
JSON = [[JSONDecoder decoder] objectWithData:data error:&JSONError];
}
#else
JSON = [[JSONDecoder decoder] objectWithData:data error:&error];
JSON = [[JSONDecoder decoder] objectWithData:data error:&JSONError];
#endif

dispatch_sync(dispatch_get_main_queue(), ^(void) {
if (error) {
if (JSONError) {
if (failure) {
failure(error);
failure(JSONError);
}
} else {
if (success) {
Expand Down

0 comments on commit da1fa38

Please sign in to comment.