Created
October 31, 2013 17:23
-
-
Save rothomp3/7253607 to your computer and use it in GitHub Desktop.
Synchronous category on AFNetworking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// AFHTTPRequestOperation+MTSynchronousRequest.m | |
// MTSyncAFNetworking | |
// | |
// Created by Robert Thompson on 10/10/13. | |
// Copyright (c) 2013 WillowTree Apps. All rights reserved. | |
// | |
#import "AFHTTPRequestOperation+MTSynchronousRequest.h" | |
@implementation AFHTTPRequestOperation (MTSynchronousRequest) | |
+ (id)sendSynchronousRequest:(NSURLRequest *)request withResponseSerializer:(AFHTTPResponseSerializer *)responseSerializer error:(NSError *__autoreleasing *)error | |
{ | |
AFHTTPRequestOperation* operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; | |
operation.responseSerializer = responseSerializer; | |
operation.completionQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); | |
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); | |
__block id response = nil; | |
__block __autoreleasing NSError** blockError = error; | |
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { | |
response = responseObject; | |
dispatch_semaphore_signal(semaphore); | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
if (blockError) | |
{ | |
*blockError = error; | |
} | |
dispatch_semaphore_signal(semaphore); | |
}]; | |
[operation start]; | |
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); | |
return response; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment