What happens to SDWebImage Cached Images in my app when the image file on the server changes?
I am using the SDWebImage
library to cache web images in my app:
https://github.com/rs/SDWebImage/blob/master/README.md
Current Usage:
[imageView setImageWithURL:[NSURL URLWithString:profilePictureUrl] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
My question is what happens once the image has been cached and then a couple of days later that image file on the server has been updated with a new image?
At the moment my application is still displaying the cached image.
I can't see in any of the documentation on setting a cache timeout or something that recognises that the file size has changed.
If anyone has experience using this particular library then any help would be greatly appreciated.
Thanks in advance.
I had a look at the source code. It processes the setImageWithURL
method like this:
- Ask the memory cache if the image is there, if yes return the image and don't go any further
- Ask the disk cache if the image is there, if yes return the image and don't go any further
- Try to download the image, return image on success else keep the placeholder image
There is no request sent to ask the remote server if there is a new version while there is something old on disk, like using ETags of the HTTP protocol.
Digging a bit deeper the cache time is set to a static value in SDImageCache.m
static NSInteger cacheMaxCacheAge = 60*60*24*7; // 1 week
it cannot be changed with a setter.
So as long as the image in the cache is valid the SDWebImage
lib won't download anything new. After a week it'll download your changed image.
You can use options
parameter.
Swift version:
imageView.sd_setImage(with: URL(string: URLWithString:profilePictureUrl),
placeholderImage: UIImage(named: "placeholder"),
options: .refreshCached,
completed: nil)
Objective-C version:
[imageView sd_setImageWithURL:[NSURL URLWithString:profilePictureUrl]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
options:SDWebImageRefreshCached
completed: nil];
Cheers!
The problem with SDImageCache's aging (which now has a setter: maxCacheAge) is that SDWebImage never really proactively does anything with it. You need to invoke cleanDisk yourself at some point to purge old data from the cache. Note: SDWebImage does invoke cleanDisk when the app terminates, but apps are not guaranteed to get a termination notification from the OS.