Kingfisher
Kingfisher copied to clipboard
Unable to cancel a task for LocalFileImageDataProvider
Check List
Thanks for considering to open an issue. Before you submit your issue, please confirm these boxes are checked.
- [x] I have read the wiki page and cheat sheet, but there is no information I need.
- [x] I have searched in existing issues, but did not find a same one.
- [x] I want to report a problem instead of asking a question. It'd better to use kingfisher tag in Stack Overflow to ask a question.
Issue Description
Unable to cancel a task for LocalFileImageDataProvider
What
while using KF to load a local image file into imageView, we found that sometimes the order of under code will cause unexpected result
imageView.kf.setImage
kf.cancelDownloadTask() <now kf task working in processingQueue>
imageView.image = someIcon
<after a short time(maybe 10ms),kf set image when process done>
now the imageView shows the wrong image
Reproduce
- upload a image to device
- add some process to options,e.g. DownsamplingImageProcessor
- load image using kf
- cancel kf
- set image with imageView.image = sth
The setImage
would give you a DownloadTask
, which represents a task related to image downloading. The same happens when you call cancelDownloadTask
. However, in a LocalFileImageDataProvider
, there is no url session downloading or any network things managed by Kingfisher, so you will never be able to cancel the image setting once it starts.
To avoid the wrong image being set, instead of setting the imageView.image
, try to keep using Kingfisher's method to set it, even it is a plain image. This should give Kingfisher a chance to identify which is the correct image to be set.
// Rewrite the last step: set image with imageView.image = sth
let provider = RawImageDataProvider(data: image.pngData()!, cacheKey: "myCacheKey")
imageView.kf.setImage(with: provider)