From db305db733da040974c12f5fc6653db4388ac230 Mon Sep 17 00:00:00 2001 From: Blake Watters Date: Wed, 6 Mar 2013 21:40:56 -0500 Subject: [PATCH] Rework delivery of AFNetworkingOperationDidStartNotification and AFNetworkingOperationDidFinishNotification to avoid crashes when logging in response to notifications --- AFNetworking/AFURLConnectionOperation.m | 34 +++++++++++-------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index 5503e68..f4196ce 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -339,20 +339,6 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat [self didChangeValueForKey:oldStateKey]; [self didChangeValueForKey:newStateKey]; [self.lock unlock]; - - dispatch_async(dispatch_get_main_queue(), ^{ - NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; - switch (state) { - case AFOperationExecutingState: - [notificationCenter postNotificationName:AFNetworkingOperationDidStartNotification object:self]; - break; - case AFOperationFinishedState: - [notificationCenter postNotificationName:AFNetworkingOperationDidFinishNotification object:self]; - break; - default: - break; - } - }); } - (NSString *)responseString { @@ -450,24 +436,34 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat - (void)operationDidStart { [self.lock lock]; - if ([self isCancelled]) { - [self finish]; - } else { + if (! [self isCancelled]) { self.connection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:NO]; - + NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; for (NSString *runLoopMode in self.runLoopModes) { [self.connection scheduleInRunLoop:runLoop forMode:runLoopMode]; [self.outputStream scheduleInRunLoop:runLoop forMode:runLoopMode]; } - + [self.connection start]; } [self.lock unlock]; + + dispatch_async(dispatch_get_main_queue(), ^{ + [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingOperationDidStartNotification object:self]; + }); + + if ([self isCancelled]) { + [self finish]; + } } - (void)finish { self.state = AFOperationFinishedState; + + dispatch_async(dispatch_get_main_queue(), ^{ + [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingOperationDidFinishNotification object:self]; + }); } - (void)cancel {