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 ()
@property (readwrite, nonatomic, assign) AFOperationState state;
@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 downloadProgress;
- (BOOL)shouldTransitionToState:(AFOperationState)state;
- (void)operationDidStart;
- (void)finish;
@end
@ -135,10 +113,10 @@ static inline BOOL AFOperationStateTransitionIsValid(AFOperationState from, AFOp
return nil;
}
self.request = urlRequest;
self.runLoopModes = [NSSet setWithObject:NSRunLoopCommonModes];
self.request = urlRequest;
self.state = AFHTTPOperationReadyState;
return self;
@ -185,11 +163,7 @@ static inline BOOL AFOperationStateTransitionIsValid(AFOperationState from, AFOp
}
- (void)setState:(AFOperationState)state {
if (self.state == state) {
return;
}
if (!AFOperationStateTransitionIsValid(self.state, state)) {
if (![self shouldTransitionToState:state]) {
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 {
[self willChangeValueForKey:@"isCancelled"];
_cancelled = cancelled;
@ -273,6 +271,10 @@ static inline BOOL AFOperationStateTransitionIsValid(AFOperationState from, AFOp
[self.connection start];
}
- (void)finish {
self.state = AFHTTPOperationFinishedState;
}
- (void)cancel {
if ([self isFinished]) {
return;
@ -285,10 +287,6 @@ static inline BOOL AFOperationStateTransitionIsValid(AFOperationState from, AFOp
[self.connection cancel];
}
- (void)finish {
self.state = AFHTTPOperationFinishedState;
}
#pragma mark - NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)__unused connection