From fefd1a77ff1ba1873ae5772fbae0681a42ef3382 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 16 Jul 2012 19:21:30 +0200 Subject: [PATCH] don't use __weak for completionBlocks. (Issue #414) We nil out the completionBlock manually in AFURLConnectionOperation. Using __weak here potentially doesn't call the set completion blocks. Restores iOS4 compatibility. Also shortens inline ifs with a ternary conditional(?:). --- AFNetworking/AFHTTPClient.m | 2 +- AFNetworking/AFHTTPRequestOperation.m | 17 +++++++----- AFNetworking/AFImageRequestOperation.m | 24 +++++++++-------- AFNetworking/AFJSONRequestOperation.m | 26 ++++++++++--------- AFNetworking/AFPropertyListRequestOperation.m | 24 +++++++++-------- AFNetworking/AFXMLRequestOperation.m | 18 +++++++------ 6 files changed, 61 insertions(+), 50 deletions(-) diff --git a/AFNetworking/AFHTTPClient.m b/AFNetworking/AFHTTPClient.m index 3bf0c11..cc7bfae 100644 --- a/AFNetworking/AFHTTPClient.m +++ b/AFNetworking/AFHTTPClient.m @@ -570,7 +570,7 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) { for (AFHTTPRequestOperation *operation in operations) { AFCompletionBlock originalCompletionBlock = [operation.completionBlock copy]; operation.completionBlock = ^{ - dispatch_queue_t queue = operation.successCallbackQueue ? operation.successCallbackQueue : dispatch_get_main_queue(); + dispatch_queue_t queue = operation.successCallbackQueue ?: dispatch_get_main_queue(); dispatch_group_async(dispatchGroup, queue, ^{ if (originalCompletionBlock) { originalCompletionBlock(); diff --git a/AFNetworking/AFHTTPRequestOperation.m b/AFNetworking/AFHTTPRequestOperation.m index 267d04d..35113fe 100644 --- a/AFNetworking/AFHTTPRequestOperation.m +++ b/AFNetworking/AFHTTPRequestOperation.m @@ -222,26 +222,29 @@ NSString * AFCreateIncompleteDownloadDirectoryPath(void) { - (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure { - __weak AFHTTPRequestOperation *weakSelf = self; + // completion block is manually nilled out in AFURLConnectionOperation to break the retain cycle. +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-retain-cycles" self.completionBlock = ^ { - if ([weakSelf isCancelled]) { + if ([self isCancelled]) { return; } - if (weakSelf.error) { + if (self.error) { if (failure) { - dispatch_async(weakSelf.failureCallbackQueue ? weakSelf.failureCallbackQueue : dispatch_get_main_queue(), ^{ - failure(weakSelf, weakSelf.error); + dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{ + failure(self, self.error); }); } } else { if (success) { - dispatch_async(weakSelf.successCallbackQueue ? weakSelf.successCallbackQueue : dispatch_get_main_queue(), ^{ - success(weakSelf, weakSelf.responseData); + dispatch_async(self.successCallbackQueue ?: dispatch_get_main_queue(), ^{ + success(self, self.responseData); }); } } }; +#pragma clang diagnostic pop } - (void)setResponseFilePath:(NSString *)responseFilePath { diff --git a/AFNetworking/AFImageRequestOperation.m b/AFNetworking/AFImageRequestOperation.m index 3b13463..e19237e 100644 --- a/AFNetworking/AFImageRequestOperation.m +++ b/AFNetworking/AFImageRequestOperation.m @@ -82,7 +82,7 @@ static dispatch_queue_t image_request_operation_processing_queue() { dispatch_async(image_request_operation_processing_queue(), ^(void) { UIImage *processedImage = imageProcessingBlock(image); - dispatch_async(requestOperation.successCallbackQueue ? requestOperation.successCallbackQueue : dispatch_get_main_queue(), ^(void) { + dispatch_async(requestOperation.successCallbackQueue ?: dispatch_get_main_queue(), ^(void) { success(operation.request, operation.response, processedImage); }); }); @@ -113,7 +113,7 @@ static dispatch_queue_t image_request_operation_processing_queue() { dispatch_async(image_request_operation_processing_queue(), ^(void) { NSImage *processedImage = imageProcessingBlock(image); - dispatch_async(requestOperation.successCallbackQueue ? requestOperation.successCallbackQueue : dispatch_get_main_queue(), ^(void) { + dispatch_async(requestOperation.successCallbackQueue ?: dispatch_get_main_queue(), ^(void) { success(operation.request, operation.response, processedImage); }); }); @@ -197,17 +197,18 @@ static dispatch_queue_t image_request_operation_processing_queue() { - (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure { - __weak AFImageRequestOperation *weakSelf = self; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-retain-cycles" self.completionBlock = ^ { - if ([weakSelf isCancelled]) { + if ([self isCancelled]) { return; } dispatch_async(image_request_operation_processing_queue(), ^(void) { - if (weakSelf.error) { + if (self.error) { if (failure) { - dispatch_async(weakSelf.failureCallbackQueue ? weakSelf.failureCallbackQueue : dispatch_get_main_queue(), ^{ - failure(weakSelf, weakSelf.error); + dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{ + failure(self, self.error); }); } } else { @@ -218,15 +219,16 @@ static dispatch_queue_t image_request_operation_processing_queue() { NSImage *image = nil; #endif - image = weakSelf.responseImage; + image = self.responseImage; - dispatch_async(weakSelf.successCallbackQueue ? weakSelf.successCallbackQueue : dispatch_get_main_queue(), ^{ - success(weakSelf, image); + dispatch_async(self.successCallbackQueue ?: dispatch_get_main_queue(), ^{ + success(self, image); }); } } }); - }; + }; +#pragma clang diagnostic pop } @end diff --git a/AFNetworking/AFJSONRequestOperation.m b/AFNetworking/AFJSONRequestOperation.m index e6968d9..f5c438e 100644 --- a/AFNetworking/AFJSONRequestOperation.m +++ b/AFNetworking/AFJSONRequestOperation.m @@ -96,38 +96,40 @@ static dispatch_queue_t json_request_operation_processing_queue() { - (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure { - __weak AFJSONRequestOperation *weakSelf = self; - self.completionBlock = ^ { - if ([weakSelf isCancelled]) { +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-retain-cycles" + self.completionBlock = ^ { + if ([self isCancelled]) { return; } - if (weakSelf.error) { + if (self.error) { if (failure) { - dispatch_async(weakSelf.failureCallbackQueue ? weakSelf.failureCallbackQueue : dispatch_get_main_queue(), ^{ - failure(weakSelf, weakSelf.error); + dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{ + failure(self, self.error); }); } } else { dispatch_async(json_request_operation_processing_queue(), ^{ - id JSON = weakSelf.responseJSON; + id JSON = self.responseJSON; if (self.JSONError) { if (failure) { - dispatch_async(weakSelf.failureCallbackQueue ? weakSelf.failureCallbackQueue : dispatch_get_main_queue(), ^{ - failure(weakSelf, weakSelf.error); + dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{ + failure(self, self.error); }); } } else { if (success) { - dispatch_async(weakSelf.successCallbackQueue ? weakSelf.successCallbackQueue : dispatch_get_main_queue(), ^{ - success(weakSelf, JSON); + dispatch_async(self.successCallbackQueue ?: dispatch_get_main_queue(), ^{ + success(self, JSON); }); } } }); } - }; + }; +#pragma clang diagnostic pop } @end diff --git a/AFNetworking/AFPropertyListRequestOperation.m b/AFNetworking/AFPropertyListRequestOperation.m index 2cf0d9d..21961d0 100644 --- a/AFNetworking/AFPropertyListRequestOperation.m +++ b/AFNetworking/AFPropertyListRequestOperation.m @@ -106,38 +106,40 @@ static dispatch_queue_t property_list_request_operation_processing_queue() { - (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure { - __weak AFPropertyListRequestOperation *weakSelf = self; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-retain-cycles" self.completionBlock = ^ { - if ([weakSelf isCancelled]) { + if ([self isCancelled]) { return; } - if (weakSelf.error) { + if (self.error) { if (failure) { - dispatch_async(weakSelf.failureCallbackQueue ? weakSelf.failureCallbackQueue : dispatch_get_main_queue(), ^{ - failure(weakSelf, weakSelf.error); + dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{ + failure(self, self.error); }); } } else { dispatch_async(property_list_request_operation_processing_queue(), ^(void) { - id propertyList = weakSelf.responsePropertyList; + id propertyList = self.responsePropertyList; if (self.propertyListError) { if (failure) { - dispatch_async(weakSelf.failureCallbackQueue ? weakSelf.failureCallbackQueue : dispatch_get_main_queue(), ^{ - failure(weakSelf, weakSelf.error); + dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{ + failure(self, self.error); }); } } else { if (success) { - dispatch_async(weakSelf.successCallbackQueue ? weakSelf.successCallbackQueue : dispatch_get_main_queue(), ^{ - success(weakSelf, propertyList); + dispatch_async(self.successCallbackQueue ?: dispatch_get_main_queue(), ^{ + success(self, propertyList); }); } } }); } - }; + }; +#pragma clang diagnostic pop } @end diff --git a/AFNetworking/AFXMLRequestOperation.m b/AFNetworking/AFXMLRequestOperation.m index dbc62a1..bdc935c 100644 --- a/AFNetworking/AFXMLRequestOperation.m +++ b/AFNetworking/AFXMLRequestOperation.m @@ -138,30 +138,32 @@ static dispatch_queue_t xml_request_operation_processing_queue() { - (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure { - __weak AFXMLRequestOperation *weakSelf = self; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Warc-retain-cycles" self.completionBlock = ^ { - if ([weakSelf isCancelled]) { + if ([self isCancelled]) { return; } dispatch_async(xml_request_operation_processing_queue(), ^(void) { - NSXMLParser *XMLParser = weakSelf.responseXMLParser; + NSXMLParser *XMLParser = self.responseXMLParser; if (self.error) { if (failure) { - dispatch_async(weakSelf.failureCallbackQueue ? weakSelf.failureCallbackQueue : dispatch_get_main_queue(), ^{ - failure(weakSelf, weakSelf.error); + dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{ + failure(self, self.error); }); } } else { if (success) { - dispatch_async(weakSelf.successCallbackQueue ? weakSelf.successCallbackQueue : dispatch_get_main_queue(), ^{ - success(weakSelf, XMLParser); + dispatch_async(self.successCallbackQueue ?: dispatch_get_main_queue(), ^{ + success(self, XMLParser); }); } } }); - }; + }; +#pragma clang diagnostic pop } @end