Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Optimizing cancel image download and let task to be canceled really. #4407

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions Tests/Tests/AFImageDownloaderTests.m
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
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