From ec8daf2c022ebb1dadfb1a705a068a64345d021b Mon Sep 17 00:00:00 2001 From: Erik Olsson Date: Wed, 29 Feb 2012 14:00:23 +0100 Subject: [PATCH 1/2] Set cancelled on main thread to avoid crashes when canceling the same connection before it has had time to cancel. --- AFNetworking/AFURLConnectionOperation.m | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index 00b898b..3870874 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -337,8 +337,11 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat - (void)cancel { [self.lock lock]; if (![self isFinished] && ![self isCancelled]) { + [self willChangeValueForKey:@"isCancelled"]; + _cancelled = YES; [super cancel]; - + [self didChangeValueForKey:@"isCancelled"]; + // Cancel the connection on the thread it runs on to prevent race conditions [self performSelector:@selector(cancelConnection) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]]; } @@ -347,8 +350,7 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat } - (void)cancelConnection { - [self willChangeValueForKey:@"isCancelled"]; - _cancelled = YES; + if (self.connection) { [self.connection cancel]; @@ -357,7 +359,7 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[self.request URL] forKey:NSURLErrorFailingURLErrorKey]; [self performSelector:@selector(connection:didFailWithError:) withObject:self.connection withObject:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:userInfo]]; } - [self didChangeValueForKey:@"isCancelled"]; + } #pragma mark - NSURLConnectionDelegate From 51e17984b5953b2d19141c06b3e52d81a0e4607f Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Wed, 29 Feb 2012 08:47:05 -0800 Subject: [PATCH 2/2] Removing excess whitespace --- AFNetworking/AFURLConnectionOperation.m | 4 ---- 1 file changed, 4 deletions(-) diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index 3870874..076d846 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -345,13 +345,10 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat // Cancel the connection on the thread it runs on to prevent race conditions [self performSelector:@selector(cancelConnection) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]]; } - [self.lock unlock]; } - (void)cancelConnection { - - if (self.connection) { [self.connection cancel]; @@ -359,7 +356,6 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[self.request URL] forKey:NSURLErrorFailingURLErrorKey]; [self performSelector:@selector(connection:didFailWithError:) withObject:self.connection withObject:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:userInfo]]; } - } #pragma mark - NSURLConnectionDelegate