Update to use the delegate mode, let the actual image loader to decide whether to mark failed or not.

This commit is contained in:
DreamPiggy 2019-04-02 14:59:21 +08:00
parent 8275274154
commit fa426f9b05
4 changed files with 50 additions and 13 deletions

View File

@ -85,4 +85,18 @@ FOUNDATION_EXPORT UIImage * _Nullable SDImageLoaderDecodeProgressiveImageData(NS
progress:(nullable SDImageLoaderProgressBlock)progressBlock
completed:(nullable SDImageLoaderCompletedBlock)completedBlock;
@optional
/**
Whether the error from image loader should be marked indded un-recoverable or not.
If this return YES, failed URL which does not using `SDWebImageRetryFailed` will be blocked into black list. Else not.
If does not implements this method, assume always return NO.
@param url The URL represent the image. Note this may not be a HTTP URL
@param error The URL's loading error, from previous `requestImageWithURL:options:context:progress:completed:` completedBlock's error.
@return Whether to block this url or not. Return YES to mark this URL as failed.
*/
- (BOOL)shouldBlockFailedURLWithURL:(nonnull NSURL *)url
error:(nonnull NSError *)error;
@end

View File

@ -100,4 +100,17 @@
return nil;
}
- (BOOL)shouldBlockFailedURLWithURL:(NSURL *)url error:(NSError *)error {
NSArray<id<SDImageLoader>> *loaders = self.loaders;
for (id<SDImageLoader> loader in loaders.reverseObjectEnumerator) {
if (![loader respondsToSelector:@selector(shouldBlockFailedURLWithURL:error:)]) {
break;
}
if ([loader canRequestImageForURL:url]) {
return [loader shouldBlockFailedURLWithURL:url error:error];
}
}
return NO;
}
@end

View File

@ -546,4 +546,25 @@ didReceiveResponse:(NSURLResponse *)response
return [self downloadImageWithURL:url options:downloaderOptions context:context progress:progressBlock completed:completedBlock];
}
- (BOOL)shouldBlockFailedURLWithURL:(NSURL *)url error:(NSError *)error {
BOOL shouldBlockFailedURL;
// Filter the error domain and check error codes
if ([error.domain isEqualToString:SDWebImageErrorDomain]) {
shouldBlockFailedURL = ( error.code == SDWebImageErrorInvalidURL
|| error.code == SDWebImageErrorBadImageData);
} else if ([error.domain isEqualToString:NSURLErrorDomain]) {
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);
} else {
shouldBlockFailedURL = NO;
}
return shouldBlockFailedURL;
}
@end

View File

@ -374,19 +374,8 @@ static id<SDImageLoader> _defaultImageLoader;
if ([self.delegate respondsToSelector:@selector(imageManager:shouldBlockFailedURL:withError:)]) {
shouldBlockFailedURL = [self.delegate imageManager:self shouldBlockFailedURL:url withError:error];
} else {
// Filter the error domain and check error codes
if ([error.domain isEqualToString:SDWebImageErrorDomain]) {
shouldBlockFailedURL = ( error.code == SDWebImageErrorInvalidURL
|| error.code == SDWebImageErrorBadImageData);
} else if ([error.domain isEqualToString:NSURLErrorDomain]) {
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);
if ([self.imageLoader respondsToSelector:@selector(shouldBlockFailedURLWithURL:error:)]) {
shouldBlockFailedURL = [self.imageLoader shouldBlockFailedURLWithURL:url error:error];
} else {
shouldBlockFailedURL = NO;
}