Refactoring state transition logic for AFURLConnectionOperation

This commit is contained in:
Mattt Thompson 2011-10-05 11:26:51 -05:00
parent b85c59516d
commit c0cba6748a

View file

@ -51,29 +51,6 @@ static inline NSString * AFKeyPathFromOperationState(AFOperationState state) {
} }
} }
static inline BOOL AFOperationStateTransitionIsValid(AFOperationState from, AFOperationState to) {
switch (from) {
case AFHTTPOperationReadyState:
switch (to) {
case AFHTTPOperationExecutingState:
return YES;
default:
return NO;
}
case AFHTTPOperationExecutingState:
switch (to) {
case AFHTTPOperationReadyState:
return NO;
default:
return YES;
}
case AFHTTPOperationFinishedState:
return NO;
default:
return YES;
}
}
@interface AFURLConnectionOperation () @interface AFURLConnectionOperation ()
@property (readwrite, nonatomic, assign) AFOperationState state; @property (readwrite, nonatomic, assign) AFOperationState state;
@property (readwrite, nonatomic, assign, getter = isCancelled) BOOL cancelled; @property (readwrite, nonatomic, assign, getter = isCancelled) BOOL cancelled;
@ -88,6 +65,7 @@ static inline BOOL AFOperationStateTransitionIsValid(AFOperationState from, AFOp
@property (readwrite, nonatomic, copy) AFURLConnectionOperationProgressBlock uploadProgress; @property (readwrite, nonatomic, copy) AFURLConnectionOperationProgressBlock uploadProgress;
@property (readwrite, nonatomic, copy) AFURLConnectionOperationProgressBlock downloadProgress; @property (readwrite, nonatomic, copy) AFURLConnectionOperationProgressBlock downloadProgress;
- (BOOL)shouldTransitionToState:(AFOperationState)state;
- (void)operationDidStart; - (void)operationDidStart;
- (void)finish; - (void)finish;
@end @end
@ -135,10 +113,10 @@ static inline BOOL AFOperationStateTransitionIsValid(AFOperationState from, AFOp
return nil; return nil;
} }
self.request = urlRequest;
self.runLoopModes = [NSSet setWithObject:NSRunLoopCommonModes]; self.runLoopModes = [NSSet setWithObject:NSRunLoopCommonModes];
self.request = urlRequest;
self.state = AFHTTPOperationReadyState; self.state = AFHTTPOperationReadyState;
return self; return self;
@ -185,11 +163,7 @@ static inline BOOL AFOperationStateTransitionIsValid(AFOperationState from, AFOp
} }
- (void)setState:(AFOperationState)state { - (void)setState:(AFOperationState)state {
if (self.state == state) { if (![self shouldTransitionToState:state]) {
return;
}
if (!AFOperationStateTransitionIsValid(self.state, state)) {
return; return;
} }
@ -214,6 +188,30 @@ static inline BOOL AFOperationStateTransitionIsValid(AFOperationState from, AFOp
} }
} }
- (BOOL)shouldTransitionToState:(AFOperationState)state {
switch (self.state) {
case AFHTTPOperationReadyState:
switch (state) {
case AFHTTPOperationExecutingState:
return YES;
case AFHTTPOperationFinishedState:
return [self isCancelled];
default:
return NO;
}
case AFHTTPOperationExecutingState:
switch (state) {
case AFHTTPOperationFinishedState:
return YES;
default:
return NO;
}
case AFHTTPOperationFinishedState:
default:
return NO;
}
}
- (void)setCancelled:(BOOL)cancelled { - (void)setCancelled:(BOOL)cancelled {
[self willChangeValueForKey:@"isCancelled"]; [self willChangeValueForKey:@"isCancelled"];
_cancelled = cancelled; _cancelled = cancelled;
@ -273,6 +271,10 @@ static inline BOOL AFOperationStateTransitionIsValid(AFOperationState from, AFOp
[self.connection start]; [self.connection start];
} }
- (void)finish {
self.state = AFHTTPOperationFinishedState;
}
- (void)cancel { - (void)cancel {
if ([self isFinished]) { if ([self isFinished]) {
return; return;
@ -285,10 +287,6 @@ static inline BOOL AFOperationStateTransitionIsValid(AFOperationState from, AFOp
[self.connection cancel]; [self.connection cancel];
} }
- (void)finish {
self.state = AFHTTPOperationFinishedState;
}
#pragma mark - NSURLConnectionDelegate #pragma mark - NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)__unused connection - (void)connection:(NSURLConnection *)__unused connection