5

I am using SDWebImage library for handling image cache. I implemented one of its method to download image from server and populating data in tableView. Here is my code for downloading image and showing in tableViewCell In cellForRowAtIndexPath, I did the following code

[cell.profileImageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:nil options:SDWebImageRefreshCached];

It works perfectly here. But when I updated image on server, the tableViewCell still shows the same image. The problem is ,its cache is not updating image. I searched with the same keywords and came across this question on StackOverflow. But couldn't resolve the issue. I also tried to clear memory and cache on viewDidDisappear

-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
    [[SDImageCache sharedImageCache]clearMemory];
    [[SDImageCache sharedImageCache]clearDisk];
}

But its not efficient way. Is there any other way to update the cache when image on server is updated ?

Community
  • 1
  • 1
Sushil Sharma
  • 2,321
  • 3
  • 29
  • 49

2 Answers2

3

You can use SDWebImageRefreshCached option to refresh concrete image, however you never know if the image was changed on the server side. So it's only your decision should you use image cache or not and when your cache should be updated.

Igor
  • 1,537
  • 10
  • 12
  • I am already using `SDWebImageRefreshCached ` in options while downloading image. But still shows the old image. – Sushil Sharma Dec 03 '15 at 12:13
  • SDWebImageRefreshCached will not ignore your cache, it only will proceed the Cache-Control http header. So server side settings can control your cache life itself. You should communicate with server admin to discuss this behaviour http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9 – Igor Dec 03 '15 at 12:21
  • Thanks. I'll try discussing. – Sushil Sharma Dec 03 '15 at 12:26
2

From SDWebImage docs :

If you don't control the image server you're using, you may not be able to change the URL when its content is updated. In such case, you may use the SDWebImageRefreshCached flag. This will slightly degrade the performance but will respect the HTTP caching control headers

[imageView sd_setImageWithURL:url
             placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                      options:SDWebImageRefreshCached];

It would defeat the purpose of Image Caching if you use this in all your code. Coz the image will be downloaded everytime. Instead you could :

  1. If you manage the server, you could setup some flags in the DB to indicate the image has changed on the server.
  2. Implement some logic to only use SDWebImageRefreshCached once in 2 days or something like that, and use the normal code all other times.

You could also clear the entire cache once in a while to force a refresh, if that's feasible.

ShahiM
  • 3,179
  • 1
  • 33
  • 58
  • Clearing entire cache is not feasible. It ignores the use of SDWebImage. and as i have added code to download image from server, it includes 'SDWebImageRefreshCached` in options. But shows old image everytime. – Sushil Sharma Dec 03 '15 at 12:15
  • have you tried calling that url in your browser and making sure that the image has actually changed? – ShahiM Dec 03 '15 at 12:16
  • Yes, It tried calling it. Also somewhere else in my app, I am downloading same image from server without using SDWebImage. Image is updated there. – Sushil Sharma Dec 03 '15 at 12:19