SDImageCache not clearing #250

Closed
JaapManenschijn opened this Issue Dec 14, 2012 · 16 comments

Comments

Projects
None yet
6 participants

With the latest version of SDWebImage, calling something like:
[[SDImageCache sharedImageCache] clearMemory] does not actually clear your image cache.

This has been keeping me busy for a bit, as I wanted to force my program to always load the image from the web and not get it from cache.
I came to a point where I used [[SDImageCache sharedImageCache] queryDiskCacheForKey: done:] ,after clearing the cache first, and it would not find anything.
Then on the next line i'd use [imageView setImageWithURL: placeholderImage: completed:], which you'd expect to load the image from the web, as the image wasn't found previously.
However, it'd still show the old image from the memory (wtf?).

I started browsing through the code and came up to the following:
Both SDImageCache and SDWebImageManager (used for downloading the image) have are singletons, as in they have a static sharedImageCache (or sharedManager) to get an instance of the class.
In the implementation of both classes, a new imageCache is created. So instead of having one imageCache across the classes, there's actually two imageCaches.

Clearing the cache through [[SDImageCache sharedImageCache] clearMemory] does clear a cache. But it's not the cache you'd want cleared.

I resolved my problem by changing the implementation of SDWebImageManager slightly.
I changed the init method a bit. I replaced the line:
_imageCache = SDImageCache.new;

with the following:
_imageCache = [SDImageCache sharedImagecache];

This way you'll have the same imageCache across both classes.
I don't know if this is the way it is supposed to be, but it made sense (atleast for my case).

Owner

rs commented Dec 14, 2012

Why don't you call [SDWebImageManager.sharedManager.imageCache clearMemory] ?

I could do that, but it does not seem to be the most logical step.
I guess for people wanting to do cache related stuff, the first place they look at is something called SDImageCache when they look through the files. Atleast, for me that was the first place to look at, and that didn't turn out to work the way you'd think it would.

It could be me, but i don't find it to be very logical.

Owner

rs commented Dec 14, 2012

SDWebImageManager can be a singleton but you can get your own instance if you want. If all instances share the same SDImageCache is shared among them it may not be what you would expect either.

Fair enough, i didn't look at it that way.
I guess i'll just clear it through the SDWebImageManager.
Wouldn't suprise me if more people ran into the same issue like i did, but i guess they'll find this issue if they can't work it out.

Thanks for your time! :)

I got this issue today.I can't find out a way to fix it.

@bpoplauschi bpoplauschi reopened this Jul 23, 2014

Collaborator

bpoplauschi commented Aug 8, 2014

@xiaguanghua can you detail on the issue, how you reproduced it, if it's 100% reproducible, which version of the library where you using, ...

@bpoplauschi bpoplauschi added this to the Future milestone Aug 8, 2014

@bpoplauschi So, I've just got this issue.
I've tried to clear my image cache in four standard ways:

  1.     [[SDImageCache sharedImageCache] clearMemory];
        [[SDImageCache sharedImageCache] cleanDisk];
    
  2.     [[SDImageCache sharedImageCache] clearMemory];
    
  3.     [SDWebImageManager.sharedManager.imageCache clearMemory];
    
  4.    [SDWebImageManager.sharedManager.imageCache cleanDisk];
    
  5. 3 and 4 together

But it didn't work the way I expect.

Collaborator

bpoplauschi commented Oct 29, 2014

@Doshipak which version of the library are you using? What do you want to achieve with "clear cache"? Get rid of the memory cache? Get rid of the disk cache?

@bpoplauschi I'am using the last version of SDWebImage (3.7.1), just updated my pods to make sure.
To be honest I didn't get the difference between "clearMemory" and "clearDisk". If it's not complicate for you can you please explain me that?

I want to do the same when I'am calling this mehod for single image:

    [[SDImageCache sharedImageCache] removeImageForKey:imageKey fromDisk:YES];

This method works fine. I want to do the same with all images stored in app device memory to load new images from server.

Collaborator

bpoplauschi commented Oct 29, 2014

The difference is clearMemory will only remove the images from the RAM cache, they will still exist on the disk, so they will be quickly loaded the next time you need them. clearDisk will erase the cached image files from the device disk, so if you need them again, they will need to be re-downloaded.

You almost found the right method for what you need. It's clearDisk not cleanDisk from SDImageCache. If you look at SDImageCache.h you can see the comments explaining the 2 methods:

/**
 * Clear all disk cached images
 * @see clearDiskOnCompletion:
 */
- (void)clearDisk;
/**
 * Remove all expired cached image from disk
 * @see cleanDiskWithCompletionBlock:
 */
- (void)cleanDisk;

@bpoplauschi Thank you man! I've missed this difference. Now everything is fine. My fault!

Collaborator

bpoplauschi commented Oct 29, 2014

👍

Collaborator

bpoplauschi commented Oct 29, 2014

I will close this issue, if there are still issues with clearing the cache, feel free to reopen or just comment here and I will reopen it.

I got a problem with my memory. I am using the SDWebimage with ProgressView. Is there a way to clear the cached memory? disk and ram?

Collaborator

bpoplauschi commented Jun 7, 2016

@handiansom you can use [[SDWebImageManager sharedManager].imageCache clearMemory] or [[SDWebImageManager sharedManager].imageCache clearDisk]

@bpoplauschi Thank you so much for your answer. It really helped me reduce memory usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment