From b85c59516d7309a2ca78a095e731a35a4d9da521 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Tue, 4 Oct 2011 00:41:32 -0500 Subject: [PATCH] Don't execute completion block if cancelled --- AFNetworking/AFHTTPRequestOperation.m | 4 ++++ AFNetworking/AFImageRequestOperation.m | 4 ++++ AFNetworking/AFURLConnectionOperation.m | 6 +++++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/AFNetworking/AFHTTPRequestOperation.m b/AFNetworking/AFHTTPRequestOperation.m index dee3043..422cd1f 100644 --- a/AFNetworking/AFHTTPRequestOperation.m +++ b/AFNetworking/AFHTTPRequestOperation.m @@ -38,6 +38,10 @@ { AFHTTPRequestOperation *operation = [[[self alloc] initWithRequest:urlRequest] autorelease]; operation.completionBlock = ^ { + if ([operation isCancelled]) { + return; + } + if (operation.error) { if (failure) { dispatch_async(dispatch_get_main_queue(), ^(void) { diff --git a/AFNetworking/AFImageRequestOperation.m b/AFNetworking/AFImageRequestOperation.m index 097acd8..70bfd59 100644 --- a/AFNetworking/AFImageRequestOperation.m +++ b/AFNetworking/AFImageRequestOperation.m @@ -53,6 +53,10 @@ static dispatch_queue_t image_request_operation_processing_queue() { AFImageRequestOperation *operation = [[[AFImageRequestOperation alloc] initWithRequest:urlRequest] autorelease]; operation.completionBlock = ^ { + if ([operation isCancelled]) { + return; + } + dispatch_async(image_request_operation_processing_queue(), ^(void) { if (operation.error) { if (failure) { diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index 0dfda3e..9a97541 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -274,6 +274,10 @@ static inline BOOL AFOperationStateTransitionIsValid(AFOperationState from, AFOp } - (void)cancel { + if ([self isFinished]) { + return; + } + [super cancel]; self.cancelled = YES; @@ -282,7 +286,7 @@ static inline BOOL AFOperationStateTransitionIsValid(AFOperationState from, AFOp } - (void)finish { - self.state = AFHTTPOperationFinishedState; + self.state = AFHTTPOperationFinishedState; } #pragma mark - NSURLConnectionDelegate