Afnetworking: Clear cached response or cached images not working, still showing old image.

Created on 14 Jun 2016  路  8Comments  路  Source: AFNetworking/AFNetworking

In my case, I need set the ImageView with the same url but different image data. I tried to clear all the cache with following code:

NSURLCache.sharedURLCache().removeAllCachedResponses()        
AFImageDownloader.defaultURLCache().removeAllCachedResponses()
AFImageDownloader.defaultInstance().imageCache?.removeAllImages()

After calling these lines, memoryUsage of imageCache and NSURLCache.sharedURLCache().currentMemoryUsage print 0 in the console. Then I updated the image on s3 with the same url and reload TableView after that. The old image returned from setImageWithURLRequest. The code below is what I use to display the image.

let imageUrl = "https://s3.ap-northeast-2.amazonaws.com/xxx/yyy.jpg"
let request = NSURLRequest(URL: NSURL(string: imageUrl)!, cachePolicy: NSURLRequestCachePolicy.ReturnCacheDataElseLoad, timeoutInterval: 10)
cell.cellImageView.setImageWithURLRequest(request, placeholderImage: UIImage(named: "placeholder"), success: { (request, response, image) in
    cell.cellImageView.image = image
}) { (request, response, error) in
    // error handling
}

Besides, if I changed the cachePolicy to NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData, I can get the updated image after clearing image caches.

stale

Most helpful comment

This code works for me:

`

[[NSURLCache sharedURLCache] removeAllCachedResponses];

AFImageDownloader *imageDownloader = [AFImageDownloader defaultInstance];
NSURLCache *urlCache = imageDownloader.sessionManager.session.configuration.URLCache;
[urlCache removeAllCachedResponses];

[imageDownloader.imageCache removeAllImages];

`

All 8 comments

I am having the same issue

I used these func in Objective-c:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[AFImageDownloader defaultURLCache] removeAllCachedResponses];
[[[AFImageDownloader defaultInstance] imageCache] removeAllImages];
AFAutoPurgingImageCache *imageCache = [[AFAutoPurgingImageCache alloc] init];
[imageCache removeAllImages];
I hope you will find these helpful.

@Dmxy
Thanks for your help, I have tried these methods with swift but seems not work. The UIImageView still uses the cached image not fetching it again from Internet.

I had the same issue. What we do to fix these is change the name of image every time when updated on server by adding timestamp in name of the image. This way the app downloads the new image.

This code works for me:

`

[[NSURLCache sharedURLCache] removeAllCachedResponses];

AFImageDownloader *imageDownloader = [AFImageDownloader defaultInstance];
NSURLCache *urlCache = imageDownloader.sessionManager.session.configuration.URLCache;
[urlCache removeAllCachedResponses];

[imageDownloader.imageCache removeAllImages];

`

Thanks, @Maxim-Inv! This is the same code in Swift and last version of Alamofire (4.5):

URLCache.shared.removeAllCachedResponses()

let imageDownloader = ImageDownloader.default
let urlCache = imageDownloader.sessionManager.session.configuration.urlCache
urlCache?.removeAllCachedResponses()

imageDownloader.imageCache?.removeAllImages()

This issue has been marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

This issue has been auto-closed because there hasn't been any activity for at least 21 days. However, we really appreciate your contribution, so thank you for that! 馃檹 Also, feel free to open a new issue if you still experience this problem 馃憤.

Was this page helpful?
0 / 5 - 0 ratings