Sushil Sharma
Sushil Sharma

Reputation: 2361

SDWebImage not updating image in cache

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 ?

Upvotes: 5

Views: 4430

Answers (2)

ShahiM
ShahiM

Reputation: 3268

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.

Upvotes: 2

Igor
Igor

Reputation: 1557

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.

Upvotes: 3

Related Questions