From 6e69da9feba5a5915215d8e94f031a23e4205fae Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Sat, 26 Jan 2013 10:47:11 -0500 Subject: [PATCH] Using early return in AFURLConnectionOperation -setState: to avoid posting notifications in lock --- AFNetworking/AFURLConnectionOperation.m | 48 +++++++++++++------------ 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/AFNetworking/AFURLConnectionOperation.m b/AFNetworking/AFURLConnectionOperation.m index 8c4edf2..cd4b93d 100644 --- a/AFNetworking/AFURLConnectionOperation.m +++ b/AFNetworking/AFURLConnectionOperation.m @@ -325,31 +325,33 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat } - (void)setState:(AFOperationState)state { - [self.lock lock]; - if (AFStateTransitionIsValid(self.state, state, [self isCancelled])) { - NSString *oldStateKey = AFKeyPathFromOperationState(self.state); - NSString *newStateKey = AFKeyPathFromOperationState(state); - - [self willChangeValueForKey:newStateKey]; - [self willChangeValueForKey:oldStateKey]; - _state = state; - [self didChangeValueForKey:oldStateKey]; - [self didChangeValueForKey:newStateKey]; - - dispatch_async(dispatch_get_main_queue(), ^{ - switch (state) { - case AFOperationExecutingState: - [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingOperationDidStartNotification object:self]; - break; - case AFOperationFinishedState: - [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingOperationDidFinishNotification object:self]; - break; - default: - break; - } - }); + if (!AFStateTransitionIsValid(self.state, state, [self isCancelled])) { + return; } + + [self.lock lock]; + NSString *oldStateKey = AFKeyPathFromOperationState(self.state); + NSString *newStateKey = AFKeyPathFromOperationState(state); + + [self willChangeValueForKey:newStateKey]; + [self willChangeValueForKey:oldStateKey]; + _state = state; + [self didChangeValueForKey:oldStateKey]; + [self didChangeValueForKey:newStateKey]; [self.lock unlock]; + + dispatch_async(dispatch_get_main_queue(), ^{ + switch (state) { + case AFOperationExecutingState: + [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingOperationDidStartNotification object:self]; + break; + case AFOperationFinishedState: + [[NSNotificationCenter defaultCenter] postNotificationName:AFNetworkingOperationDidFinishNotification object:self]; + break; + default: + break; + } + }); } - (NSString *)responseString {