Feature request: A cancel function in ImageDataProvider protocol
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
What
Currently, we have func data(handler: @escaping (Result<Data, Error>) -> Void) in ImageDataProvider. It's enough for most use cases. But when we call kf.setImage() second time before the previous data(handler:) return its result, we have no way to cancel the previous data(handler:). If data(handler:) is a heavy task, we'd better to cancel it manually instead of waiting for its deinit.
Thanks for the suggestion.
I believe it is a good direction for Kingfisher to evolve. But for now, the image setting method is returning a DownloadTask and it is a non-nil value only when download progress is actually started by ImageDownloader. The download task is managed in Kingfisher and you can call task.cancel() to stop the downloading task.
That says a simple cancel function in ImageDataProvider is not enough to provide a useful API. There is no "task management" for ImageDataProvider, so when setting the image again, it is not possible to track which of the providing task should be cancelled. Changing it may require a public interface change for the setting image method (at least we need a more generic return type, instead of the current DownloadTask). So I guess it won't happen soon until we are prepared to make a major upgrade.
I will tag it with v8 and see if I have a chance to handle it.