Thanks for considering to open an issue. Before you submit your issue, please confirm these boxes are checked.
I want to know: can Kingfisher auto retry when download failed?
I search everywhere but not found.. Thank you!
Nope! Think about that, Kingfisher is able to auto retry when download fails, and you offer a wrong URL... Kingfisher will keep doing useless work.
In fact, Kingfisher downloads image from a URL which is offered by you,
when it succeeded, you will get the image and error info from CompletionHandler(image, error, cacheType, imageURL), and of course the error is nil.
when it failed, image will be nil, you can get the error information from error.
So far, errors information offered by Kingfisher are as follows:
10000 - badData
10001 - notModified
10002 - invalidStatusCode
10003 - notCached
20000 - invalidURL
30000 - downloadCancelledBeforeStarting
@wudijimao
As @ixongju mentioned, there is no built-in auto retry support now in Kingfisher. (However, it is possible if we can limit the auto retry count to a number so it will not keep retrying forever).
For me, since I always use Kingfisher in a table view or collection view, every time the cell be loaded, it will try to load the image. More or less, it behaves like a "retry" for downloading images. And it works well for me without any user complain about it. (and we also have a mechanism to allow the user to tap on the image to load.)
But auto retry itself is an interesting feature, I think I'd like to implement it in the future.
@ixongju Thanks for the information about the errors. Currently (version 4.x), the error types are not so useful. In branch 5.0, we can have fully detailed errors to show what exactly happened inside Kingfisher. You can find it in this file: https://github.com/onevcat/Kingfisher/blob/5.0/Sources/General/KingfisherError.swift
And the new version now is already in beta. If everything goes well, there would be no more API changes so you could have a try on it.
If I decide to add support for built-in auto retry, it will be done after version 5 released.
Hi! I've written some code to implement a setImage with retry based on @onevcat 's code in another thread. Feel free to use it. In my case I retry only when an http error is received.
extension KingfisherWrapper where Base: UIImageView {
@discardableResult
func setImageWithRetry(
with resource: URL?,
retry: Int = 3,
retryDelay: TimeInterval = 3.0,
placeholder: Placeholder? = nil,
options: KingfisherOptionsInfo? = nil,
progressBlock: DownloadProgressBlock? = nil,
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask? {
let downloadTask = setImage(with: resource,
placeholder: placeholder,
options: options, progressBlock: progressBlock) { result in
switch result {
case .success:
completionHandler?(result)
case .failure(let error):
if retry > 0,
case .responseError(let reason) = error,
case .invalidHTTPStatusCode = reason {
DispatchQueue.main.asyncAfter(deadline: .now() + retryDelay) {
self.setImageWithRetry(
with: resource,
retry: retry - 1,
retryDelay: retryDelay,
placeholder: placeholder,
options: options,
progressBlock: progressBlock,
completionHandler: completionHandler)
}
} else {
completionHandler?(result)
}
}
}
return downloadTask
}
}
Most helpful comment
@wudijimao
As @ixongju mentioned, there is no built-in auto retry support now in Kingfisher. (However, it is possible if we can limit the auto retry count to a number so it will not keep retrying forever).
For me, since I always use Kingfisher in a table view or collection view, every time the cell be loaded, it will try to load the image. More or less, it behaves like a "retry" for downloading images. And it works well for me without any user complain about it. (and we also have a mechanism to allow the user to tap on the image to load.)
But auto retry itself is an interesting feature, I think I'd like to implement it in the future.
@ixongju Thanks for the information about the errors. Currently (version 4.x), the error types are not so useful. In branch 5.0, we can have fully detailed errors to show what exactly happened inside Kingfisher. You can find it in this file: https://github.com/onevcat/Kingfisher/blob/5.0/Sources/General/KingfisherError.swift
And the new version now is already in beta. If everything goes well, there would be no more API changes so you could have a try on it.
If I decide to add support for built-in auto retry, it will be done after version 5 released.