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
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];

View file

@ -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) {

View file

@ -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) {

View file

@ -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) {

View file

@ -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);
}

View file

@ -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