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(?:).
This commit is contained in:
parent
2b2317907e
commit
fefd1a77ff
6 changed files with 61 additions and 50 deletions
|
|
@ -570,7 +570,7 @@ static void AFNetworkReachabilityReleaseCallback(const void *info) {
|
||||||
for (AFHTTPRequestOperation *operation in operations) {
|
for (AFHTTPRequestOperation *operation in operations) {
|
||||||
AFCompletionBlock originalCompletionBlock = [operation.completionBlock copy];
|
AFCompletionBlock originalCompletionBlock = [operation.completionBlock copy];
|
||||||
operation.completionBlock = ^{
|
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, ^{
|
dispatch_group_async(dispatchGroup, queue, ^{
|
||||||
if (originalCompletionBlock) {
|
if (originalCompletionBlock) {
|
||||||
originalCompletionBlock();
|
originalCompletionBlock();
|
||||||
|
|
|
||||||
|
|
@ -222,26 +222,29 @@ NSString * AFCreateIncompleteDownloadDirectoryPath(void) {
|
||||||
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
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 = ^ {
|
self.completionBlock = ^ {
|
||||||
if ([weakSelf isCancelled]) {
|
if ([self isCancelled]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (weakSelf.error) {
|
if (self.error) {
|
||||||
if (failure) {
|
if (failure) {
|
||||||
dispatch_async(weakSelf.failureCallbackQueue ? weakSelf.failureCallbackQueue : dispatch_get_main_queue(), ^{
|
dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{
|
||||||
failure(weakSelf, weakSelf.error);
|
failure(self, self.error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (success) {
|
if (success) {
|
||||||
dispatch_async(weakSelf.successCallbackQueue ? weakSelf.successCallbackQueue : dispatch_get_main_queue(), ^{
|
dispatch_async(self.successCallbackQueue ?: dispatch_get_main_queue(), ^{
|
||||||
success(weakSelf, weakSelf.responseData);
|
success(self, self.responseData);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
#pragma clang diagnostic pop
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setResponseFilePath:(NSString *)responseFilePath {
|
- (void)setResponseFilePath:(NSString *)responseFilePath {
|
||||||
|
|
|
||||||
|
|
@ -82,7 +82,7 @@ static dispatch_queue_t image_request_operation_processing_queue() {
|
||||||
dispatch_async(image_request_operation_processing_queue(), ^(void) {
|
dispatch_async(image_request_operation_processing_queue(), ^(void) {
|
||||||
UIImage *processedImage = imageProcessingBlock(image);
|
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);
|
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) {
|
dispatch_async(image_request_operation_processing_queue(), ^(void) {
|
||||||
NSImage *processedImage = imageProcessingBlock(image);
|
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);
|
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
|
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
||||||
{
|
{
|
||||||
__weak AFImageRequestOperation *weakSelf = self;
|
#pragma clang diagnostic push
|
||||||
|
#pragma clang diagnostic ignored "-Warc-retain-cycles"
|
||||||
self.completionBlock = ^ {
|
self.completionBlock = ^ {
|
||||||
if ([weakSelf isCancelled]) {
|
if ([self isCancelled]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch_async(image_request_operation_processing_queue(), ^(void) {
|
dispatch_async(image_request_operation_processing_queue(), ^(void) {
|
||||||
if (weakSelf.error) {
|
if (self.error) {
|
||||||
if (failure) {
|
if (failure) {
|
||||||
dispatch_async(weakSelf.failureCallbackQueue ? weakSelf.failureCallbackQueue : dispatch_get_main_queue(), ^{
|
dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{
|
||||||
failure(weakSelf, weakSelf.error);
|
failure(self, self.error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -218,15 +219,16 @@ static dispatch_queue_t image_request_operation_processing_queue() {
|
||||||
NSImage *image = nil;
|
NSImage *image = nil;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
image = weakSelf.responseImage;
|
image = self.responseImage;
|
||||||
|
|
||||||
dispatch_async(weakSelf.successCallbackQueue ? weakSelf.successCallbackQueue : dispatch_get_main_queue(), ^{
|
dispatch_async(self.successCallbackQueue ?: dispatch_get_main_queue(), ^{
|
||||||
success(weakSelf, image);
|
success(self, image);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
#pragma clang diagnostic pop
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
||||||
|
|
@ -96,38 +96,40 @@ static dispatch_queue_t json_request_operation_processing_queue() {
|
||||||
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
||||||
{
|
{
|
||||||
__weak AFJSONRequestOperation *weakSelf = self;
|
#pragma clang diagnostic push
|
||||||
self.completionBlock = ^ {
|
#pragma clang diagnostic ignored "-Warc-retain-cycles"
|
||||||
if ([weakSelf isCancelled]) {
|
self.completionBlock = ^ {
|
||||||
|
if ([self isCancelled]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (weakSelf.error) {
|
if (self.error) {
|
||||||
if (failure) {
|
if (failure) {
|
||||||
dispatch_async(weakSelf.failureCallbackQueue ? weakSelf.failureCallbackQueue : dispatch_get_main_queue(), ^{
|
dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{
|
||||||
failure(weakSelf, weakSelf.error);
|
failure(self, self.error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
dispatch_async(json_request_operation_processing_queue(), ^{
|
dispatch_async(json_request_operation_processing_queue(), ^{
|
||||||
id JSON = weakSelf.responseJSON;
|
id JSON = self.responseJSON;
|
||||||
|
|
||||||
if (self.JSONError) {
|
if (self.JSONError) {
|
||||||
if (failure) {
|
if (failure) {
|
||||||
dispatch_async(weakSelf.failureCallbackQueue ? weakSelf.failureCallbackQueue : dispatch_get_main_queue(), ^{
|
dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{
|
||||||
failure(weakSelf, weakSelf.error);
|
failure(self, self.error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (success) {
|
if (success) {
|
||||||
dispatch_async(weakSelf.successCallbackQueue ? weakSelf.successCallbackQueue : dispatch_get_main_queue(), ^{
|
dispatch_async(self.successCallbackQueue ?: dispatch_get_main_queue(), ^{
|
||||||
success(weakSelf, JSON);
|
success(self, JSON);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
#pragma clang diagnostic pop
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
||||||
|
|
@ -106,38 +106,40 @@ static dispatch_queue_t property_list_request_operation_processing_queue() {
|
||||||
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
||||||
{
|
{
|
||||||
__weak AFPropertyListRequestOperation *weakSelf = self;
|
#pragma clang diagnostic push
|
||||||
|
#pragma clang diagnostic ignored "-Warc-retain-cycles"
|
||||||
self.completionBlock = ^ {
|
self.completionBlock = ^ {
|
||||||
if ([weakSelf isCancelled]) {
|
if ([self isCancelled]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (weakSelf.error) {
|
if (self.error) {
|
||||||
if (failure) {
|
if (failure) {
|
||||||
dispatch_async(weakSelf.failureCallbackQueue ? weakSelf.failureCallbackQueue : dispatch_get_main_queue(), ^{
|
dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{
|
||||||
failure(weakSelf, weakSelf.error);
|
failure(self, self.error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
dispatch_async(property_list_request_operation_processing_queue(), ^(void) {
|
dispatch_async(property_list_request_operation_processing_queue(), ^(void) {
|
||||||
id propertyList = weakSelf.responsePropertyList;
|
id propertyList = self.responsePropertyList;
|
||||||
|
|
||||||
if (self.propertyListError) {
|
if (self.propertyListError) {
|
||||||
if (failure) {
|
if (failure) {
|
||||||
dispatch_async(weakSelf.failureCallbackQueue ? weakSelf.failureCallbackQueue : dispatch_get_main_queue(), ^{
|
dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{
|
||||||
failure(weakSelf, weakSelf.error);
|
failure(self, self.error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (success) {
|
if (success) {
|
||||||
dispatch_async(weakSelf.successCallbackQueue ? weakSelf.successCallbackQueue : dispatch_get_main_queue(), ^{
|
dispatch_async(self.successCallbackQueue ?: dispatch_get_main_queue(), ^{
|
||||||
success(weakSelf, propertyList);
|
success(self, propertyList);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
#pragma clang diagnostic pop
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
||||||
|
|
@ -138,30 +138,32 @@ static dispatch_queue_t xml_request_operation_processing_queue() {
|
||||||
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
- (void)setCompletionBlockWithSuccess:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
|
||||||
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
|
||||||
{
|
{
|
||||||
__weak AFXMLRequestOperation *weakSelf = self;
|
#pragma clang diagnostic push
|
||||||
|
#pragma clang diagnostic ignored "-Warc-retain-cycles"
|
||||||
self.completionBlock = ^ {
|
self.completionBlock = ^ {
|
||||||
if ([weakSelf isCancelled]) {
|
if ([self isCancelled]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatch_async(xml_request_operation_processing_queue(), ^(void) {
|
dispatch_async(xml_request_operation_processing_queue(), ^(void) {
|
||||||
NSXMLParser *XMLParser = weakSelf.responseXMLParser;
|
NSXMLParser *XMLParser = self.responseXMLParser;
|
||||||
|
|
||||||
if (self.error) {
|
if (self.error) {
|
||||||
if (failure) {
|
if (failure) {
|
||||||
dispatch_async(weakSelf.failureCallbackQueue ? weakSelf.failureCallbackQueue : dispatch_get_main_queue(), ^{
|
dispatch_async(self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{
|
||||||
failure(weakSelf, weakSelf.error);
|
failure(self, self.error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (success) {
|
if (success) {
|
||||||
dispatch_async(weakSelf.successCallbackQueue ? weakSelf.successCallbackQueue : dispatch_get_main_queue(), ^{
|
dispatch_async(self.successCallbackQueue ?: dispatch_get_main_queue(), ^{
|
||||||
success(weakSelf, XMLParser);
|
success(self, XMLParser);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
#pragma clang diagnostic pop
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue