Created
August 8, 2012 09:41
-
-
Save corosukeK/3293807 to your computer and use it in GitHub Desktop.
HTTPLoader
This file contains 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
// | |
// HTTPLoader.m | |
// HTTPLoader | |
// | |
// Created by Keisuke Kimura on 2012/08/08. | |
// Copyright (c) 2012年 Keisuke Kimura. All rights reserved. | |
// | |
#import "HTTPLoader.h" | |
static NSOperationQueue* operationQueue; | |
@implementation HTTPLoader | |
+ (NSOperationQueue*)operationQueue | |
{ | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
operationQueue = [[NSOperationQueue alloc] init]; | |
[operationQueue setMaxConcurrentOperationCount:5]; | |
}); | |
return operationQueue; | |
} | |
+ (void)loadAsync:(NSURLRequest *)request complete:(CompleteBlock)completeBlock | |
{ | |
__block NSBlockOperation* operation = [NSBlockOperation blockOperationWithBlock:^{ | |
NSData* responseData = nil; | |
NSURLResponse* response = nil; | |
NSError* error = nil; | |
responseData = [NSURLConnection sendSynchronousRequest:request | |
returningResponse:&response | |
error:&error]; | |
if(!operation.isCancelled){ | |
completeBlock(responseData,error); | |
} | |
}]; | |
[[HTTPLoader operationQueue] addOperation:operation]; | |
} | |
+ (void)cancelAll | |
{ | |
[[HTTPLoader operationQueue] cancelAllOperations]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment