Merge branch 'master' into experimental-pause-resume

Conflicts:
	AFNetworking/AFHTTPRequestOperation.h
	AFNetworking/AFHTTPRequestOperation.m
This commit is contained in:
Mattt Thompson 2012-04-24 20:27:37 -07:00
commit 701a60a10f
6 changed files with 58 additions and 42 deletions

View file

@ -579,35 +579,42 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) {
progressBlock:(void (^)(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations))progressBlock progressBlock:(void (^)(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations))progressBlock
completionBlock:(void (^)(NSArray *operations))completionBlock completionBlock:(void (^)(NSArray *operations))completionBlock
{ {
__block dispatch_group_t dispatchGroup = dispatch_group_create();
dispatch_retain(dispatchGroup);
NSBlockOperation *batchedOperation = [NSBlockOperation blockOperationWithBlock:^{ NSBlockOperation *batchedOperation = [NSBlockOperation blockOperationWithBlock:^{
if (completionBlock) { dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^{
dispatch_async(dispatch_get_main_queue(), ^{ if (completionBlock) {
completionBlock(operations); completionBlock(operations);
}); }
} });
dispatch_release(dispatchGroup);
}]; }];
[self.operationQueue addOperation:batchedOperation];
NSPredicate *finishedOperationPredicate = [NSPredicate predicateWithFormat:@"isFinished == YES"]; NSPredicate *finishedOperationPredicate = [NSPredicate predicateWithFormat:@"isFinished == YES"];
for (AFHTTPRequestOperation *operation in operations) { for (AFHTTPRequestOperation *operation in operations) {
AFCompletionBlock originalCompletionBlock = [[operation.completionBlock copy] autorelease]; AFCompletionBlock originalCompletionBlock = [[operation.completionBlock copy] autorelease];
operation.completionBlock = ^{ operation.completionBlock = ^{
if (progressBlock) { dispatch_queue_t queue = operation.successCallbackQueue ? operation.successCallbackQueue : dispatch_get_main_queue();
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_group_async(dispatchGroup, queue, ^{
progressBlock([[batchedOperation.dependencies filteredArrayUsingPredicate:finishedOperationPredicate] count], [batchedOperation.dependencies count]); if (originalCompletionBlock) {
}); originalCompletionBlock();
} }
if (originalCompletionBlock) { if (progressBlock) {
originalCompletionBlock(); progressBlock([[operations filteredArrayUsingPredicate:finishedOperationPredicate] count], [operations count]);
} }
dispatch_group_leave(dispatchGroup);
});
}; };
dispatch_group_enter(dispatchGroup);
[batchedOperation addDependency:operation]; [batchedOperation addDependency:operation];
[self enqueueHTTPRequestOperation:operation]; [self enqueueHTTPRequestOperation:operation];
} }
[self.operationQueue addOperation:batchedOperation];
} }
#pragma mark - #pragma mark -
@ -757,7 +764,7 @@ static inline NSString * AFMultipartFormFinalBoundary() {
} }
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:fileURL]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:fileURL];
[request setCachePolicy:NSURLCacheStorageNotAllowed]; [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
NSURLResponse *response = nil; NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error]; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error];

View file

@ -119,7 +119,7 @@ static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) {
dispatch_release(_failureCallbackQueue); dispatch_release(_failureCallbackQueue);
_failureCallbackQueue = NULL; _failureCallbackQueue = NULL;
} }
[super dealloc]; [super dealloc];
} }
@ -177,6 +177,7 @@ static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) {
if (successCallbackQueue != _successCallbackQueue) { if (successCallbackQueue != _successCallbackQueue) {
if (_successCallbackQueue) { if (_successCallbackQueue) {
dispatch_release(_successCallbackQueue); dispatch_release(_successCallbackQueue);
_successCallbackQueue = NULL;
} }
if (successCallbackQueue) { if (successCallbackQueue) {
@ -190,6 +191,7 @@ static NSString * AFStringFromIndexSet(NSIndexSet *indexSet) {
if (failureCallbackQueue != _failureCallbackQueue) { if (failureCallbackQueue != _failureCallbackQueue) {
if (_failureCallbackQueue) { if (_failureCallbackQueue) {
dispatch_release(_failureCallbackQueue); dispatch_release(_failureCallbackQueue);
_failureCallbackQueue = NULL;
} }
if (failureCallbackQueue) { if (failureCallbackQueue) {

View file

@ -79,12 +79,17 @@ static dispatch_queue_t image_request_operation_processing_queue() {
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success) { if (success) {
UIImage *image = responseObject; UIImage *image = responseObject;
if (imageProcessingBlock) { 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) { } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure) { if (failure) {
@ -106,12 +111,17 @@ static dispatch_queue_t image_request_operation_processing_queue() {
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success) { if (success) {
NSImage *image = responseObject; NSImage *image = responseObject;
if (imageProcessingBlock) { 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) { } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure) { if (failure) {

View file

@ -114,7 +114,7 @@ static dispatch_queue_t json_request_operation_processing_queue() {
}); });
} }
} else { } else {
dispatch_async(json_request_operation_processing_queue(), ^(void) { dispatch_async(json_request_operation_processing_queue(), ^{
id JSON = self.responseJSON; id JSON = self.responseJSON;
if (self.JSONError) { if (self.JSONError) {

View file

@ -130,18 +130,9 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat
+ (void)networkRequestThreadEntryPoint:(id)__unused object { + (void)networkRequestThreadEntryPoint:(id)__unused object {
do { do {
NSAutoreleasePool *exceptionPool = [[NSAutoreleasePool alloc] init]; NSAutoreleasePool *runLoopPool = [[NSAutoreleasePool alloc] init];
NSException *caughtException = nil; [[NSRunLoop currentRunLoop] run];
@try { [runLoopPool drain];
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];
} while (YES); } while (YES);
} }

View file

@ -115,6 +115,7 @@ static char kAFImageRequestOperationObjectKey;
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
if ([[urlRequest URL] isEqual:[[self.af_imageRequestOperation request] URL]]) { if ([[urlRequest URL] isEqual:[[self.af_imageRequestOperation request] URL]]) {
self.image = responseObject; self.image = responseObject;
self.af_imageRequestOperation = nil;
} }
if (success) { if (success) {
@ -123,13 +124,16 @@ static char kAFImageRequestOperationObjectKey;
[[[self class] af_sharedImageCache] cacheImage:responseObject forRequest:urlRequest]; [[[self class] af_sharedImageCache] cacheImage:responseObject forRequest:urlRequest];
self.af_imageRequestOperation = nil;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if ([[urlRequest URL] isEqual:[[self.af_imageRequestOperation request] URL]]) {
self.af_imageRequestOperation = nil;
}
if (failure) { if (failure) {
failure(operation.request, operation.response, error); failure(operation.request, operation.response, error);
} }
self.af_imageRequestOperation = nil;
}]; }];
self.af_imageRequestOperation = requestOperation; self.af_imageRequestOperation = requestOperation;
@ -168,7 +172,9 @@ static inline NSString * AFImageCacheKeyFromURLRequest(NSURLRequest *request) {
- (void)cacheImage:(UIImage *)image - (void)cacheImage:(UIImage *)image
forRequest:(NSURLRequest *)request forRequest:(NSURLRequest *)request
{ {
[self setObject:image forKey:AFImageCacheKeyFromURLRequest(request)]; if (image && request) {
[self setObject:image forKey:AFImageCacheKeyFromURLRequest(request)];
}
} }
@end @end