I am using SDWebImage library to download images from server. https://github.com/rs/SDWebImage
SDWebImage not able update the cached image when image updated on server with the same url.
I am using SDWebImage library to download images from server. https://github.com/rs/SDWebImage
SDWebImage not able update the cached image when image updated on server with the same url.
Go to the line number 176 in the file SDWebImageManager.m and change this line
to below code.
For me it worked like a charm.
Swift 4 Simply use the following function in the SDWebImage Library :
This function will delete the saved Cash in memory and Disk , after that just Upload your new Image at the same and it will work perfectly.
SDWebImage
does some caching by default, so it would be better to use a new URL if the image changes. So, for instance, if you have control over the URL and can change it every time the image has changed, you could do that.If that's not the case, try using
SDWebImageRefreshCached
in theoptions
field in order to respect HTTP cache control headers, like this:See more here
Update: I've actually written an entire guide about cache, including cache validation https://kean.github.io/blog/image-caching
SDWebImage
usesNSURLCache
when you setSDWebImageRefreshCached
option. Apple's URL loading system implements HTTP cache, including cached responses validation. HTTP cache is quite complex, however there are many beginners guides on HTTP caching:Basically, the server needs to include some of HTTP cache control headers in each response. There are many different strategies that can be used to implement revalidation. You may use either
Last-Modified
orETag
. This way each time the client sends a request it will automatically include in your request eitherLast-Modified
orETag
value from previously cached response. If the image hasn't change the server will respond with status code 302 (not modified) andNSURLConnection/NSURLSession
will transparently give you a cached response fromNSURLCache
. You don't have to download the data again, buy you still need to check with the server each time you make a request.You can also specify an expiration date using HTTP cache control. If the expiration mechanism is used,
NSURLConnection/NSURLSession
will not revalidate cached response until it is not expired.For more info about HTTP cache control see the links above. HTTP cache is a universal cache mechanism that should be used whenever possible.
I would recommend to use Nuke framework for image loading (disclaimer: writted by me). It uses
NSURLCache
by default while still having a memory cache that holds decompressed images.If URL is not changed there is now way for SDWebImage to know that image has been changed on the server.
Here is a code in swift 3 to refresh cache everytime