What is the default Kingfisher cache behavior?

I am using the Kingfisher library for the purpose of caching images in UICollectionView cells.

I noticed that the call kf_setImage

(assuming the name of the cell is titled listCell

and its ImageView is called imgMain

) is like this:

listCell.imgMain.kf.setImage(with: URL(string: "MY IMAGE URL"),
                             placeholder: UIImage(named: "PLACEHOLDER IMAGE"))

      

works great, it caches the image and displays it when the cell is re-deleted (when scrolling up and down, I can see the image directly without overloading it) , but when I try to update the collection view (remember the API with the same exact parameters as means it will return the same image urls) the images have been re-uploaded! I am assuming the images have already been cached.

To make it clearer, this link contains a gif image that describes what I am running into.

So why are the images loading again? is this the default caching behavior in Kingfisher? Is there any configuration that needs to be edited in order to behave as I expect?

I have read the library documentation but unfortunately I could not find any useful information related to what I am asking.

+3


source to share


2 answers


Kingfisher uses the entire url as its default cache key, so you have to make sure you have the most SAME url string for images every time you request your API. If something like a timestamp or version number is appended to the URLs, the cache will fail. (for example, http://example.com/image.png?time=123

and http://example.com/image.png?time=456

will be recognized as two different cache keys, although you can get the same image from it).

This is the only case I could do for your operation. If this is true for you, you can create ImageResource

to explicitly specify the cache key (which should be associated with your original url, but get rid of the added things):



let r = ImageResource(downloadURL: yourUrl, cacheKey: key)
imageView.kf.setImage(with: r, placeholder: yourImage)

      

+7


source


According to the behavior of Kingfisher that I saw earlier, for loading cells, it initially gets images and stores them in cache.

Every time you scroll through it, handle it changing the image, reloading, getting from the cache, etc. In your Pull to refresh you reload your datasource, so Kingfisher will try to reload the cache again, you can avoid manipulating the cache manually with Kingfisher or by specifying in the method parameters kf_setImage

.

If you see the method signature kf_setImage

:

public func kf_setImage(with resource: Resource?,
                          placeholder: Image? = nil,
                              options: KingfisherOptionsInfo? = nil,
                        progressBlock: DownloadProgressBlock? = nil,
                    completionHandler: CompletionHandler? = nil) -> RetrieveImageTask

      

It has a default parameter options

nil

. You can specify in these parameters, for example onlyFromCache

, and if it is installed, Kingfisher will try to fetch the image from the cache and not from the network.



imageView.kf.setImage(with: url, options: [.onlyFromCache])

      

Another way if customized ImageCache

and ImageDownloader

according to your requirements.

But you need to be careful, because when you do Pull to Refresh, the default behavior is to first display the images from the cache and update it as it comes from the network, so you need to handle it somehow.

I hope this helps you

+1


source







All Articles