Permalink
Browse files

Use serial queue instead of concurrent for file writing.

  • Loading branch information...
1 parent e09b169 commit 202be849bf5b82d8b2e8a673e8c06b82109e7d33 @samvermette samvermette committed Apr 15, 2012
Showing with 20 additions and 19 deletions.
  1. +20 −19 SVHTTPRequest/SVHTTPRequest.m
@@ -36,6 +36,7 @@ @interface SVHTTPRequest ()
@property (nonatomic, strong) NSDictionary *operationParameters;
@property (nonatomic, strong) NSString *operationSavePath;
+@property (nonatomic, assign) dispatch_queue_t saveDataDispatchQueue;
@property (nonatomic, assign) dispatch_group_t saveDataDispatchGroup;
@property (nonatomic, copy) void (^operationCompletionBlock)(id response, NSError *error);
@property (nonatomic, copy) void (^operationProgressBlock)(float progress);
@@ -65,12 +66,13 @@ @implementation SVHTTPRequest
// private properties
@synthesize operationRequest, operationData, operationConnection, operationParameters, operationFileHandle, state;
@synthesize operationSavePath, operationCompletionBlock, operationProgressBlock, timeoutTimer;
-@synthesize expectedContentLength, receivedContentLength, saveDataDispatchGroup;
+@synthesize expectedContentLength, receivedContentLength, saveDataDispatchGroup, saveDataDispatchQueue;
@synthesize requestPath, userAgent;
- (void)dealloc {
[operationConnection cancel];
dispatch_release(saveDataDispatchGroup);
+ dispatch_release(saveDataDispatchQueue);
}
#pragma mark - Convenience Methods
@@ -124,8 +126,9 @@ - (SVHTTPRequest*)initWithAddress:(NSString*)urlString method:(SVHTTPRequestMeth
self.operationSavePath = savePath;
self.operationParameters = parameters;
self.saveDataDispatchGroup = dispatch_group_create();
+ self.saveDataDispatchQueue = dispatch_queue_create("com.samvermette.SVHTTPRequest", DISPATCH_QUEUE_SERIAL);
- self.operationRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
+ self.operationRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[self.operationRequest setTimeoutInterval:kSVHTTPRequestTimeoutInterval];
if(method == SVHTTPRequestMethodGET)
@@ -320,21 +323,19 @@ - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespon
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
- dispatch_group_async(self.saveDataDispatchGroup, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
- @synchronized (self) {
- if(self.operationSavePath) {
- @try { //writeData: can throw exception when there's no disk space. Give an error, don't crash
- [self.operationFileHandle writeData:data];
- }
- @catch (NSException *exception) {
- [self.operationConnection cancel];
- NSError *writeError = [NSError errorWithDomain:@"SVHTTPRequestWriteError" code:0 userInfo:exception.userInfo];
- [self callCompletionBlockWithResponse:nil error:writeError];
- }
+ dispatch_group_async(self.saveDataDispatchGroup, self.saveDataDispatchQueue, ^{
+ if(self.operationSavePath) {
+ @try { //writeData: can throw exception when there's no disk space. Give an error, don't crash
+ [self.operationFileHandle writeData:data];
+ }
+ @catch (NSException *exception) {
+ [self.operationConnection cancel];
+ NSError *writeError = [NSError errorWithDomain:@"SVHTTPRequestWriteError" code:0 userInfo:exception.userInfo];
+ [self callCompletionBlockWithResponse:nil error:writeError];
}
- else
- [self.operationData appendData:data];
}
+ else
+ [self.operationData appendData:data];
});
if(self.operationProgressBlock) {
@@ -392,10 +393,10 @@ @implementation NSString (SVHTTPRequest)
- (NSString*)encodedURLParameterString {
NSString *result = (__bridge_transfer NSString*)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
- (__bridge CFStringRef)self,
- NULL,
- CFSTR(":/=,!$&'()*+;[]@#?"),
- kCFStringEncodingUTF8);
+ (__bridge CFStringRef)self,
+ NULL,
+ CFSTR(":/=,!$&'()*+;[]@#?"),
+ kCFStringEncodingUTF8);
return result;
}

0 comments on commit 202be84

Please sign in to comment.