Adding @try/@catch with autorelease pool for exceptions

Wrapping -start in lock
This commit is contained in:
Mattt Thompson 2011-12-08 12:42:59 -06:00
parent 680fcd5071
commit a466c27886

View file

@ -119,9 +119,18 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat
+ (void)networkRequestThreadEntryPoint:(id)__unused object {
do {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSAutoreleasePool *exceptionPool = [[NSAutoreleasePool alloc] init];
NSException *caughtException = nil;
@try {
NSAutoreleasePool *runLoopPool = [[NSAutoreleasePool alloc] init];
[[NSRunLoop currentRunLoop] run];
[pool drain];
[runLoopPool drain];
}
@catch(NSException *e) { caughtException = e; }
if(caughtException) {
NSLog(NSLocalizedString(@"Unhandled exception on %@ networking thread: %@, userInfo: %@", nil), NSStringFromClass([self class]), caughtException, [caughtException userInfo]);
}
[exceptionPool drain];
} while (YES);
}
@ -280,13 +289,13 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat
}
- (void)start {
if (![self isReady]) {
return;
}
[self.lock lock];
if ([self isReady]) {
self.state = AFHTTPOperationExecutingState;
[self performSelector:@selector(operationDidStart) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:YES modes:[self.runLoopModes allObjects]];
[self performSelector:@selector(operationDidStart) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]];
}
[self.lock unlock];
}
- (void)operationDidStart {
@ -318,12 +327,14 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat
self.cancelled = YES;
if (self.connection) {
[self.connection cancel];
// We must send this delegate protcol message ourselves since the above [self.connection cancel] causes the connection to never send another message to its delegate.
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.lock unlock];
}