Skip to content

Instantly share code, notes, and snippets.

@fmtonakai
Created August 14, 2012 05:31
Show Gist options
  • Save fmtonakai/3346601 to your computer and use it in GitHub Desktop.
Save fmtonakai/3346601 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
typedef void(^HTTPLoaderCompleteBlock)(NSData *data, NSError *error);
@interface HTTPLoader : NSObject
+(void)loadAsync:(NSURLRequest *)request complete:(HTTPLoaderCompleteBlock)completion;
@end
#import "HTTPLoader.h"
@implementation HTTPLoader
+(void)loadAsync:(NSURLRequest *)request complete:(HTTPLoaderCompleteBlock)completion
{
dispatch_queue_t currentQueue = dispatch_get_current_queue();
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error = nil;
NSURLResponse *response = nil;
NSData *receivedData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
dispatch_async(currentQueue, ^{
if (completion) {
completion(receivedData, error);
}
});
});
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment