Skip to content

Commit

Permalink
Optimizing cancel image download and let task to be canceled really. (A…
Browse files Browse the repository at this point in the history
…FNetworking#4407)

* optimizing cancel image download task

* update test case
  • Loading branch information
kinarobin authored Mar 27, 2020
1 parent a5a9436 commit a1f208b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 21 additions & 0 deletions Tests/Tests/AFImageDownloaderTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,27 @@ - (void)testThatItCanDownloadAndCancelAndDownloadAgain {
[self waitForExpectationsWithCommonTimeout];
}

- (void)testThatCancelImageDownloadItShouldCancelImmediately {
[self.downloader.imageCache removeAllImages];

NSString *imageURLString = @"https://secure.gravatar.com/avatar/5a105e8b9d40e1329780d62ea2265d8a?d=identicon";
AFImageDownloadReceipt *receipt = [self.downloader
downloadImageForURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imageURLString]]
success:nil
failure:nil];
[self.downloader cancelTaskForImageDownloadReceipt:receipt];

XCTestExpectation *expectation = [self expectationWithDescription:@"Image download should be cancelled immediately"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
UIImage *cacheImage = [self.downloader.imageCache imageWithIdentifier:imageURLString];
XCTAssertNil(cacheImage);
[expectation fulfill];
});

[self waitForExpectationsWithCommonTimeout];
}


#pragma mark - Threading
- (void)testThatItAlwaysCallsTheSuccessHandlerOnTheMainQueue {
XCTestExpectation *expectation = [self expectationWithDescription:@"image download should succeed"];
Expand Down
2 changes: 1 addition & 1 deletion UIKit+AFNetworking/AFImageDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ - (void)cancelTaskForImageDownloadReceipt:(AFImageDownloadReceipt *)imageDownloa
}
}

if (mergedTask.responseHandlers.count == 0 && mergedTask.task.state == NSURLSessionTaskStateSuspended) {
if (mergedTask.responseHandlers.count == 0) {
[mergedTask.task cancel];
[self removeMergedTaskWithURLIdentifier:URLIdentifier];
}
Expand Down

0 comments on commit a1f208b

Please sign in to comment.