Creating custom GCD queues for JSON and image request processing
This commit is contained in:
parent
a6ad381407
commit
da1fa38bd4
2 changed files with 26 additions and 8 deletions
|
|
@ -33,6 +33,15 @@ static inline CGSize kAFImageRequestRoundedCornerRadii(CGSize imageSize) {
|
||||||
return CGSizeMake(dimension, dimension);
|
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
|
@implementation AFImageRequestOperation
|
||||||
|
|
||||||
+ (id)operationWithRequest:(NSURLRequest *)urlRequest
|
+ (id)operationWithRequest:(NSURLRequest *)urlRequest
|
||||||
|
|
@ -47,7 +56,7 @@ static inline CGSize kAFImageRequestRoundedCornerRadii(CGSize imageSize) {
|
||||||
success:(void (^)(UIImage *image))success
|
success:(void (^)(UIImage *image))success
|
||||||
{
|
{
|
||||||
return [self operationWithRequest:urlRequest completion:^(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error) {
|
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;
|
UIImage *image = nil;
|
||||||
if ([[UIScreen mainScreen] scale] == 2.0) {
|
if ([[UIScreen mainScreen] scale] == 2.0) {
|
||||||
CGImageRef imageRef = [[UIImage imageWithData:data] CGImage];
|
CGImageRef imageRef = [[UIImage imageWithData:data] CGImage];
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,15 @@
|
||||||
|
|
||||||
#include <Availability.h>
|
#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
|
@implementation AFJSONRequestOperation
|
||||||
|
|
||||||
+ (id)operationWithRequest:(NSURLRequest *)urlRequest
|
+ (id)operationWithRequest:(NSURLRequest *)urlRequest
|
||||||
|
|
@ -62,23 +71,23 @@
|
||||||
failure(error);
|
failure(error);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
|
dispatch_async(json_request_operation_processing_queue(), ^(void) {
|
||||||
id JSON = nil;
|
id JSON = nil;
|
||||||
|
NSError *JSONError = nil;
|
||||||
#if __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_4_3
|
#if __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_4_3
|
||||||
if ([NSJSONSerialization class]) {
|
if ([NSJSONSerialization class]) {
|
||||||
JSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
|
JSON = [NSJSONSerialization JSONObjectWithData:data options:0 error:&JSONError];
|
||||||
} else {
|
} else {
|
||||||
JSON = [[JSONDecoder decoder] objectWithData:data error:&error];
|
JSON = [[JSONDecoder decoder] objectWithData:data error:&JSONError];
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
JSON = [[JSONDecoder decoder] objectWithData:data error:&error];
|
JSON = [[JSONDecoder decoder] objectWithData:data error:&JSONError];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
dispatch_sync(dispatch_get_main_queue(), ^(void) {
|
dispatch_sync(dispatch_get_main_queue(), ^(void) {
|
||||||
if (error) {
|
if (JSONError) {
|
||||||
if (failure) {
|
if (failure) {
|
||||||
failure(error);
|
failure(JSONError);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (success) {
|
if (success) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue