Kingfisher icon indicating copy to clipboard operation
Kingfisher copied to clipboard

Feature request: A cancel function in ImageDataProvider protocol

Open superk589 opened this issue 3 years ago • 1 comments

Check List

Thanks for considering to open an issue. Before you submit your issue, please confirm these boxes are checked.

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.

superk589 avatar Aug 11 '22 07:08 superk589

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.

onevcat avatar Sep 07 '22 12:09 onevcat