Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rothomp3/7253607 to your computer and use it in GitHub Desktop.
Save rothomp3/7253607 to your computer and use it in GitHub Desktop.
Synchronous category on AFNetworking
//
// 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