diff --git a/AFNetworking/AFHTTPClient.m b/AFNetworking/AFHTTPClient.m index 4e3bbd1..0e136cb 100644 --- a/AFNetworking/AFHTTPClient.m +++ b/AFNetworking/AFHTTPClient.m @@ -579,35 +579,42 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) { progressBlock:(void (^)(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations))progressBlock completionBlock:(void (^)(NSArray *operations))completionBlock { + __block dispatch_group_t dispatchGroup = dispatch_group_create(); + dispatch_retain(dispatchGroup); NSBlockOperation *batchedOperation = [NSBlockOperation blockOperationWithBlock:^{ - if (completionBlock) { - dispatch_async(dispatch_get_main_queue(), ^{ + dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^{ + if (completionBlock) { completionBlock(operations); - }); - } + } + }); + dispatch_release(dispatchGroup); }]; - - [self.operationQueue addOperation:batchedOperation]; - + NSPredicate *finishedOperationPredicate = [NSPredicate predicateWithFormat:@"isFinished == YES"]; for (AFHTTPRequestOperation *operation in operations) { AFCompletionBlock originalCompletionBlock = [[operation.completionBlock copy] autorelease]; operation.completionBlock = ^{ - if (progressBlock) { - dispatch_async(dispatch_get_main_queue(), ^{ - progressBlock([[batchedOperation.dependencies filteredArrayUsingPredicate:finishedOperationPredicate] count], [batchedOperation.dependencies count]); - }); - } - - if (originalCompletionBlock) { - originalCompletionBlock(); - } + dispatch_queue_t queue = operation.successCallbackQueue ? operation.successCallbackQueue : dispatch_get_main_queue(); + dispatch_group_async(dispatchGroup, queue, ^{ + if (originalCompletionBlock) { + originalCompletionBlock(); + } + + if (progressBlock) { + progressBlock([[operations filteredArrayUsingPredicate:finishedOperationPredicate] count], [operations count]); + } + + dispatch_group_leave(dispatchGroup); + }); }; + dispatch_group_enter(dispatchGroup); [batchedOperation addDependency:operation]; + [self enqueueHTTPRequestOperation:operation]; } + [self.operationQueue addOperation:batchedOperation]; } #pragma mark - @@ -757,7 +764,7 @@ static inline NSString * AFMultipartFormFinalBoundary() { } NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:fileURL]; - [request setCachePolicy:NSURLCacheStorageNotAllowed]; + [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; NSURLResponse *response = nil; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error]; diff --git a/AFNetworking/AFHTTPRequestOperation.m b/AFNetworking/AFHTTPRequestOperation.m index be53c61..483fbc9 100644 --- a/AFNetworking/AFHTTPRequestOperation.m +++ b/AFNetworking/AFHTTPRequestOperation.m @@ -119,7 +119,7 @@ static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) { dispatch_release(_failureCallbackQueue); _failureCallbackQueue = NULL; } - + [super dealloc]; } @@ -177,6 +177,7 @@ static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) { if (successCallbackQueue != _successCallbackQueue) { if (_successCallbackQueue) { dispatch_release(_successCallbackQueue); + _successCallbackQueue = NULL; } if (successCallbackQueue) { @@ -190,6 +191,7 @@ static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) { if (failureCallbackQueue != _failureCallbackQueue) { if (_failureCallbackQueue) { dispatch_release(_failureCallbackQueue); + _failureCallbackQueue = NULL; } if (failureCallbackQueue) { diff --git a/AFNetworking/AFImageRequestOperation.m b/AFNetworking/AFImageRequestOperation.m index 3365a20..3821046 100644 --- a/AFNetworking/AFImageRequestOperation.m +++ b/AFNetworking/AFImageRequestOperation.m @@ -79,12 +79,17 @@ static dispatch_queue_t image_request_operation_processing_queue() { [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { if (success) { UIImage *image = responseObject; - if (imageProcessingBlock) { - image = imageProcessingBlock(image); + dispatch_async(image_request_operation_processing_queue(), ^(void) { + UIImage *processedImage = imageProcessingBlock(image); + + dispatch_async(dispatch_get_main_queue(), ^(void) { + success(operation.request, operation.response, processedImage); + }); + }); + } else { + success(operation.request, operation.response, image); } - - success(operation.request, operation.response, image); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { if (failure) { @@ -106,12 +111,17 @@ static dispatch_queue_t image_request_operation_processing_queue() { [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { if (success) { NSImage *image = responseObject; - if (imageProcessingBlock) { - image = imageProcessingBlock(image); + dispatch_async(image_request_operation_processing_queue(), ^(void) { + NSImage *processedImage = imageProcessingBlock(image); + + dispatch_async(dispatch_get_main_queue(), ^(void) { + success(operation.request, operation.response, processedImage); + }); + }); + } else { + success(operation.request, operation.response, image); } - - success(operation.request, operation.response, image); } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { if (failure) { diff --git a/AFNetworking/AFJSONRequestOperation.m b/AFNetworking/AFJSONRequestOperation.m index 451705e..9d40368 100644 --- a/AFNetworking/AFJSONRequestOperation.m +++ b/AFNetworking/AFJSONRequestOperation.m @@ -114,7 +114,7 @@ static dispatch_queue_t json_request_operation_processing_queue() { }); } } else { - dispatch_async(json_request_operation_processing_queue(), ^(void) { + dispatch_async(json_request_operation_processing_queue(), ^{ id JSON = self.responseJSON; if (self.JSONError) { diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index 2b60952..2f1f5b3 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -130,18 +130,9 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat + (void)networkRequestThreadEntryPoint:(id)__unused object { do { - NSAutoreleasePool *exceptionPool = [[NSAutoreleasePool alloc] init]; - NSException *caughtException = nil; - @try { - NSAutoreleasePool *runLoopPool = [[NSAutoreleasePool alloc] init]; - [[NSRunLoop currentRunLoop] run]; - [runLoopPool drain]; - } - @catch(NSException *e) { caughtException = e; } - if(caughtException) { - NSLog(NSLocalizedString(@"Unhandled exception on %@ networking thread: %@, userInfo: %@", nil), NSStringFromClass([self class]), caughtException, [caughtException userInfo]); - } - [exceptionPool drain]; + NSAutoreleasePool *runLoopPool = [[NSAutoreleasePool alloc] init]; + [[NSRunLoop currentRunLoop] run]; + [runLoopPool drain]; } while (YES); } diff --git a/AFNetworking/UIImageView+AFNetworking.m b/AFNetworking/UIImageView+AFNetworking.m index 5310e71..ac1f708 100644 --- a/AFNetworking/UIImageView+AFNetworking.m +++ b/AFNetworking/UIImageView+AFNetworking.m @@ -115,6 +115,7 @@ static char kAFImageRequestOperationObjectKey; [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { if ([[urlRequest URL] isEqual:[[self.af_imageRequestOperation request] URL]]) { self.image = responseObject; + self.af_imageRequestOperation = nil; } if (success) { @@ -123,13 +124,16 @@ static char kAFImageRequestOperationObjectKey; [[[self class] af_sharedImageCache] cacheImage:responseObject forRequest:urlRequest]; - self.af_imageRequestOperation = nil; + } failure:^(AFHTTPRequestOperation *operation, NSError *error) { + if ([[urlRequest URL] isEqual:[[self.af_imageRequestOperation request] URL]]) { + self.af_imageRequestOperation = nil; + } + if (failure) { failure(operation.request, operation.response, error); } - self.af_imageRequestOperation = nil; }]; self.af_imageRequestOperation = requestOperation; @@ -168,7 +172,9 @@ static inline NSString * AFImageCacheKeyFromURLRequest(NSURLRequest *request) { - (void)cacheImage:(UIImage *)image forRequest:(NSURLRequest *)request { - [self setObject:image forKey:AFImageCacheKeyFromURLRequest(request)]; + if (image && request) { + [self setObject:image forKey:AFImageCacheKeyFromURLRequest(request)]; + } } @end