Move the block failed url logic into a separate method, filter the error codes not inside NSURLErrorDomain

This commit is contained in:
DreamPiggy 2019-01-08 14:05:05 +08:00
parent cf1d993b97
commit a1106d51f0
1 changed files with 28 additions and 14 deletions

View File

@ -247,20 +247,7 @@ static id<SDImageLoader> _defaultImageLoader;
// Image refresh hit the NSURLCache cache, do not call the completion block
} else if (error) {
[self callCompletionBlockForOperation:strongOperation completion:completedBlock error:error url:url];
BOOL shouldBlockFailedURL;
// Check whether we should block failed url
if ([self.delegate respondsToSelector:@selector(imageManager:shouldBlockFailedURL:withError:)]) {
shouldBlockFailedURL = [self.delegate imageManager:self shouldBlockFailedURL:url withError:error];
} else {
shouldBlockFailedURL = ( error.code != NSURLErrorNotConnectedToInternet
&& error.code != NSURLErrorCancelled
&& error.code != NSURLErrorTimedOut
&& error.code != NSURLErrorInternationalRoamingOff
&& error.code != NSURLErrorDataNotAllowed
&& error.code != NSURLErrorCannotFindHost
&& error.code != NSURLErrorCannotConnectToHost
&& error.code != NSURLErrorNetworkConnectionLost);
}
BOOL shouldBlockFailedURL = [self shouldBlockFailedURLWithURL:url error:error];
if (shouldBlockFailedURL) {
SD_LOCK(self.failedURLsLock);
@ -380,6 +367,33 @@ static id<SDImageLoader> _defaultImageLoader;
});
}
- (BOOL)shouldBlockFailedURLWithURL:(nonnull NSURL *)url
error:(nonnull NSError *)error {
// Check whether we should block failed url
BOOL shouldBlockFailedURL;
if ([self.delegate respondsToSelector:@selector(imageManager:shouldBlockFailedURL:withError:)]) {
shouldBlockFailedURL = [self.delegate imageManager:self shouldBlockFailedURL:url withError:error];
} else {
NSString *domain = error.domain;
NSInteger code = error.code;
if ([domain isEqualToString:NSURLErrorDomain]) {
shouldBlockFailedURL = ( code != NSURLErrorNotConnectedToInternet
&& code != NSURLErrorCancelled
&& code != NSURLErrorTimedOut
&& code != NSURLErrorInternationalRoamingOff
&& code != NSURLErrorDataNotAllowed
&& code != NSURLErrorCannotFindHost
&& code != NSURLErrorCannotConnectToHost
&& code != NSURLErrorNetworkConnectionLost);
} else {
// Custom loader, don't do extra error code check
shouldBlockFailedURL = NO;
}
}
return shouldBlockFailedURL;
}
- (SDWebImageContext *)processedContextWithContext:(SDWebImageContext *)context {
SDWebImageMutableContext *mutableContext = [SDWebImageMutableContext dictionary];