Created
August 14, 2012 05:31
-
-
Save fmtonakai/3346601 to your computer and use it in GitHub Desktop.
HTTPLoader @ http://satoshi.blogs.com/life/2012/08/block.html
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
#import <Foundation/Foundation.h> | |
typedef void(^HTTPLoaderCompleteBlock)(NSData *data, NSError *error); | |
@interface HTTPLoader : NSObject | |
+(void)loadAsync:(NSURLRequest *)request complete:(HTTPLoaderCompleteBlock)completion; | |
@end |
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
#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