Set cancelled on main thread to avoid crashes when canceling the same connection before it has had time to cancel.

This commit is contained in:
Erik Olsson 2012-02-29 14:00:23 +01:00
parent e0831197d3
commit ec8daf2c02

View file

@ -337,8 +337,11 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat
- (void)cancel { - (void)cancel {
[self.lock lock]; [self.lock lock];
if (![self isFinished] && ![self isCancelled]) { if (![self isFinished] && ![self isCancelled]) {
[self willChangeValueForKey:@"isCancelled"];
_cancelled = YES;
[super cancel]; [super cancel];
[self didChangeValueForKey:@"isCancelled"];
// Cancel the connection on the thread it runs on to prevent race conditions // 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 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 { - (void)cancelConnection {
[self willChangeValueForKey:@"isCancelled"];
_cancelled = YES;
if (self.connection) { if (self.connection) {
[self.connection cancel]; [self.connection cancel];
@ -357,7 +359,7 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[self.request URL] forKey:NSURLErrorFailingURLErrorKey]; 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 performSelector:@selector(connection:didFailWithError:) withObject:self.connection withObject:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:userInfo]];
} }
[self didChangeValueForKey:@"isCancelled"];
} }
#pragma mark - NSURLConnectionDelegate #pragma mark - NSURLConnectionDelegate