RP89
RP89

Reputation: 121

Show updated images in the same url using Kingfisher ios

I am using Kingfisher for loading image view with images from Url. Sometimes, the same url will updated with new image. So I am using below code to load the imageview,

profileImage.kf.setImage(with: profileUrl, placeholder: #imageLiteral(resourceName: "profile_1"), options: [.fromMemoryCacheOrRefresh], progressBlock: nil, completionHandler: nil)

But the image is not getting updated. Only the old image is showing. Why this happens ? In Kingfisher documentation, it is stated that, " fromMemoryCacheOrRefresh can be used to display changeable image behind same url, while avoiding download it again and again "

Upvotes: 2

Views: 3252

Answers (2)

Aakarsh
Aakarsh

Reputation: 1

I have used the following approach to solve the issue. By doing this, it will set the cached image as the placeholder and will replace the image only if there is any change and changing won't be noticeable as it would be done in background. Let me know if you need any further help. Note:here I'm setting image of a button

                    self.avatar.kf.setBackgroundImage(with: URL(string: imagelink), for: UIControl.State()){
                        result in
                        switch result{
                        case .success(let value): 
                        self.avatar.kf.setBackgroundImage(with: URL(string: imagelink), for: UIControl.State(), placeholder: value.image,options: [.forceRefresh])
                        print("avatar set")
                            break
                        case .failure: print("not found in cache")
                            break
                        }

Upvotes: 0

onevcat
onevcat

Reputation: 4631

Kingfisher does not support server cache mechanism. It just uses the whole URL as the local cache key. As long as you use the same URL, you will get the same image from the cache (if it was cached).

So, if your server provides different images under the same URL, we suggest you to ask your server to add a query to the URL for different versions. These URLs: "https://example.com/image.png?v=1" and "https://example.com/image.png?v=2" represent different images in Kingfisher and it could work well. At the same time, access to the "https://example.com/image.png?v=2" would navigate you to the "https://example.com/image.png" data for most implementation of the server.

You can find more information on this page:

https://github.com/onevcat/Kingfisher/wiki/FAQ#does-kingfisher-support-server-cache-tags-like-e-tag-or-last-modified

Upvotes: 6

Related Questions