Skip to content

Commit 940f991

Browse files
authored
Merge pull request SDWebImage#3497 from dreampiggy/bugfix/same_url_failed_callback_times
Fix the issue when multiple request for same url failed, the completedBlock will callback more times
2 parents 9cf8d12 + 9bc5207 commit 940f991

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

SDWebImage/Core/SDWebImageDownloaderOperation.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,8 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didComp
601601
CGSize imageSize = image.size;
602602
if (imageSize.width == 0 || imageSize.height == 0) {
603603
NSString *description = image == nil ? @"Downloaded image decode failed" : @"Downloaded image has 0 pixels";
604-
[self callCompletionBlocksWithError:[NSError errorWithDomain:SDWebImageErrorDomain code:SDWebImageErrorBadImageData userInfo:@{NSLocalizedDescriptionKey : description}]];
604+
NSError *error = [NSError errorWithDomain:SDWebImageErrorDomain code:SDWebImageErrorBadImageData userInfo:@{NSLocalizedDescriptionKey : description}];
605+
[self callCompletionBlockWithToken:token image:nil imageData:nil error:error finished:YES];
605606
} else {
606607
[self callCompletionBlockWithToken:token image:image imageData:imageData error:nil finished:YES];
607608
}

Tests/Tests/SDWebImageDownloaderTests.m

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,35 @@ - (void)test31ThatMultipleRequestForSameURLNeverSkipCallback {
830830
[self waitForExpectations:expectations timeout:kAsyncTestTimeout * 2];
831831
}
832832

833+
834+
- (void)test31ThatMultipleRequestForSameURLFailedCallback {
835+
// See #3493, silly bug
836+
NSURL *url = [NSURL fileURLWithPath:@"/dev/null"]; // Always fail url
837+
NSMutableArray<XCTestExpectation *> *expectations = [NSMutableArray arrayWithCapacity:100];
838+
__block void (^recursiveBlock)(int);
839+
void (^mainBlock)(int) = ^(int i) {
840+
if (i > 200) return;
841+
NSString *desc = [NSString stringWithFormat:@"Failed url with index %d should callback error", i];
842+
XCTestExpectation *expectation = [self expectationWithDescription:desc];
843+
[expectations addObject:expectation];
844+
// Delay 0.01s ~ 0.99s for each download request, simulate the real-world call site
845+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(i * 10000000ull)), dispatch_get_main_queue(), ^{
846+
[SDWebImageDownloader.sharedDownloader downloadImageWithURL:url completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
847+
if (error) {
848+
expect(error.code).equal(SDWebImageErrorBadImageData);
849+
[expectation fulfill];
850+
}
851+
}];
852+
});
853+
recursiveBlock(i+1);
854+
};
855+
recursiveBlock = mainBlock;
856+
recursiveBlock(0);
857+
858+
[self waitForExpectations:expectations timeout:kAsyncTestTimeout * 2];
859+
}
860+
861+
833862
#pragma mark - SDWebImageLoader
834863
- (void)testCustomImageLoaderWorks {
835864
XCTestExpectation *expectation = [self expectationWithDescription:@"Custom image not works"];

0 commit comments

Comments
 (0)