Skip to content

Commit

Permalink
Merge pull request AFNetworking#1545 from pavolas/cancel_bug
Browse files Browse the repository at this point in the history
Fix a non-deterministic failure in [AFURLConnectionOperation cancel]
  • Loading branch information
mattt committed Nov 17, 2013
2 parents cc55d6b + 3af90de commit fb3b20c
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions AFNetworking/AFURLConnectionOperation.m
Expand Up @@ -439,10 +439,6 @@ - (void)operationDidStart {
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingOperationDidStartNotification object:self];
});

if ([self isCancelled]) {
[self finish];
}
}

- (void)finish {
Expand Down Expand Up @@ -474,9 +470,15 @@ - (void)cancelConnection {
}
NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:userInfo];

if (![self isFinished] && self.connection) {
[self.connection cancel];
[self performSelector:@selector(connection:didFailWithError:) withObject:self.connection withObject:error];
if (![self isFinished]) {
if (self.connection) {
[self.connection cancel];
[self performSelector:@selector(connection:didFailWithError:) withObject:self.connection withObject:error];
} else {
// If 'cancel' is called immediately after 'start', there is a race condition where self.connection has not yet been instantiated. We still want the callback to go through and the error to be set.
self.error = error;
[self finish];
}
}
}

Expand Down

0 comments on commit fb3b20c

Please sign in to comment.