Created
August 8, 2012 10:14
-
-
Save corosukeK/3294013 to your computer and use it in GitHub Desktop.
HTTPLoader
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
// | |
// 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; | |
static dispatch_queue_t serialQueue; | |
@implementation HTTPLoader | |
+ (NSOperationQueue*)operationQueue | |
{ | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
operationQueue = [[NSOperationQueue alloc] init]; | |
[operationQueue setMaxConcurrentOperationCount:5]; | |
}); | |
return operationQueue; | |
} | |
+ (dispatch_queue_t)serialDispatchQueue | |
{ | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
serialQueue = dispatch_queue_create("com.corosuke.httploader.serial",NULL); | |
}); | |
return serialQueue; | |
} | |
+ (void)loadAsync:(NSURLRequest *)request complete:(CompleteBlock)completeBlock | |
{ | |
dispatch_sync([HTTPLoader serialDispatchQueue], ^{ | |
__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 | |
{ | |
dispatch_sync([HTTPLoader serialDispatchQueue], ^{ | |
[[HTTPLoader operationQueue] cancelAllOperations]; | |
}); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment