Skip to content

Commit fa079d9

Browse files
sampagemattt
authored andcommitted
Squashed commit of the following:
commit 35b4e1abd727ab9a5fb1489d73d4985a18efd138 Author: Mattt Thompson <[email protected]> Date: Thu Dec 5 14:22:49 2013 -0800 Minor formatting commit 36007b0f147dba1db2d46d26389a76aa339ecf0d Merge: c25c49a c7ab4ad Author: Mattt Thompson <[email protected]> Date: Thu Dec 5 14:11:40 2013 -0800 Merge branch 'AFURLConnectionOperation-isFinished-without-being-started-fix' of github.com:sampage/AFNetworking into sampage-AFURLConnectionOperation-isFinished-without-being-started-fix commit c7ab4ad Author: Sam Page <[email protected]> Date: Wed Dec 4 19:23:48 2013 +1100 Adds unit tests to cover cancelling an operation before it has begun. Rolls back previous test changes to logic of cancelling after the operation has started. commit 412d826 Author: Sam Page <[email protected]> Date: Wed Dec 4 19:23:01 2013 +1100 Previous change would mean operations in-flight would not be cancelled. On cancel, check whether the operation is currently executing and if so, cancel the operation immediately. commit a522bd7 Author: Sam Page <[email protected]> Date: Wed Dec 4 18:08:01 2013 +1100 Updates AFHTTPRequestOperationTests that cover cancelling to adhere to new logic of operations receiving -start after being cancelled commit fbe385f Author: Sam Page <[email protected]> Date: Wed Dec 4 18:07:02 2013 +1100 Removing explicit call to set state to AFOperationFinishedState. Happens as a product of cancelConnection and setting here will mean the connection is not cancelled. commit b71eaa8 Author: Sam Page <[email protected]> Date: Wed Dec 4 17:10:23 2013 +1100 Matching conditionals formatting commit 270066c Author: Sam Page <[email protected]> Date: Wed Dec 4 16:35:43 2013 +1100 Checks whether AFURLConnectionOperation isCancelled during start, if so, cancel the connection and mark state as AFOperationFinishedState. Removes connection cancelling call from NSOperation’s cancel method. Fixes an issue with iOS 6.1 where cancelling an operation before it had been started would crash after logging “AFHTTPRequestOperation xxx went isFinished=YES without being started by the queue it is in” commit 72295f6 Author: Sam Page <[email protected]> Date: Wed Dec 4 16:31:07 2013 +1100 Removes cancelled flag from AFURLConnectionOperation, instead relying on the default NSOperation isCancelled. Removes KVO notifications for isCancelled flag to adhere to docs: “..Support for cancellation is voluntary but encouraged and your own code should not have to send KVO notifications for this key path..” Signed-off-by: Mattt Thompson <[email protected]>
1 parent c25c49a commit fa079d9

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

AFNetworking/AFURLConnectionOperation.m

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat
124124

125125
@interface AFURLConnectionOperation ()
126126
@property (readwrite, nonatomic, assign) AFOperationState state;
127-
@property (readwrite, nonatomic, assign, getter = isCancelled) BOOL cancelled;
128127
@property (readwrite, nonatomic, strong) NSRecursiveLock *lock;
129128
@property (readwrite, nonatomic, strong) NSURLConnection *connection;
130129
@property (readwrite, nonatomic, strong) NSURLRequest *request;
@@ -421,7 +420,9 @@ - (BOOL)isConcurrent {
421420

422421
- (void)start {
423422
[self.lock lock];
424-
if ([self isReady]) {
423+
if ([self isCancelled]) {
424+
[self performSelector:@selector(cancelConnection) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]];
425+
} else if ([self isReady]) {
425426
self.state = AFOperationExecutingState;
426427

427428
[self performSelector:@selector(operationDidStart) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]];
@@ -460,13 +461,11 @@ - (void)finish {
460461
- (void)cancel {
461462
[self.lock lock];
462463
if (![self isFinished] && ![self isCancelled]) {
463-
[self willChangeValueForKey:@"isCancelled"];
464-
_cancelled = YES;
465464
[super cancel];
466-
[self didChangeValueForKey:@"isCancelled"];
467-
468-
// Cancel the connection on the thread it runs on to prevent race conditions
469-
[self performSelector:@selector(cancelConnection) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]];
465+
466+
if ([self isExecuting]) {
467+
[self performSelector:@selector(cancelConnection) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:NO modes:[self.runLoopModes allObjects]];
468+
}
470469
}
471470
[self.lock unlock];
472471
}
@@ -699,7 +698,6 @@ - (id)initWithCoder:(NSCoder *)decoder {
699698
}
700699

701700
self.state = (AFOperationState)[decoder decodeIntegerForKey:NSStringFromSelector(@selector(state))];
702-
self.cancelled = [decoder decodeBoolForKey:NSStringFromSelector(@selector(isCancelled))];
703701
self.response = [decoder decodeObjectForKey:NSStringFromSelector(@selector(response))];
704702
self.error = [decoder decodeObjectForKey:NSStringFromSelector(@selector(error))];
705703
self.responseData = [decoder decodeObjectForKey:NSStringFromSelector(@selector(responseData))];
@@ -723,7 +721,6 @@ - (void)encodeWithCoder:(NSCoder *)coder {
723721
break;
724722
}
725723

726-
[coder encodeBool:[self isCancelled] forKey:NSStringFromSelector(@selector(isCancelled))];
727724
[coder encodeObject:self.response forKey:NSStringFromSelector(@selector(response))];
728725
[coder encodeObject:self.error forKey:NSStringFromSelector(@selector(error))];
729726
[coder encodeObject:self.responseData forKey:NSStringFromSelector(@selector(responseData))];

Tests/Tests/AFHTTPRequestOperationTests.m

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,52 @@ - (void)testThatCancellationOfRequestOperationInvokesFailureCompletionBlock {
153153
expect(blockError.code).will.equal(NSURLErrorCancelled);
154154
}
155155

156+
- (void)testThatCancellationOfRequestBeforeStartingRequestSetsError {
157+
__block NSError *blockError = nil;
158+
159+
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/5" relativeToURL:self.baseURL]];
160+
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
161+
162+
// AFHTTPOperation currently does not have a default response serializer
163+
[operation setResponseSerializer:[AFHTTPResponseSerializer serializer]];
164+
165+
[operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
166+
blockError = error;
167+
}];
168+
169+
[operation cancel];
170+
[operation start];
171+
172+
expect([operation isCancelled]).will.beTruthy();
173+
expect([operation isFinished]).will.beTruthy();
174+
expect([operation isExecuting]).will.beFalsy();
175+
176+
expect(operation.error).willNot.beNil();
177+
expect(blockError).willNot.beNil();
178+
expect(blockError.code).will.equal(NSURLErrorCancelled);
179+
}
180+
181+
- (void)testThatCancellationOfRequestBeforeStartingRequestSetsErrorInvokesFailureCompletionBlock {
182+
__block NSError *blockError = nil;
183+
184+
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/delay/5" relativeToURL:self.baseURL]];
185+
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
186+
187+
// AFHTTPOperation currently does not have a default response serializer
188+
[operation setResponseSerializer:[AFHTTPResponseSerializer serializer]];
189+
190+
[operation setCompletionBlockWithSuccess:nil failure:^(AFHTTPRequestOperation *operation, NSError *error) {
191+
blockError = error;
192+
}];
193+
194+
[operation cancel];
195+
[operation start];
196+
197+
expect(operation.error).willNot.beNil();
198+
expect(blockError).willNot.beNil();
199+
expect(blockError.code).will.equal(NSURLErrorCancelled);
200+
}
201+
156202
- (void)testThat500StatusCodeInvokesFailureCompletionBlockWithErrorOnFailure {
157203
__block NSError *blockError = nil;
158204

0 commit comments

Comments
 (0)