Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear memory and disk cache for images loaded using Kingfisherin UITableView?

I need to free all the memory occupied by images fetched using Kingfisher. I have a UITableView that store a lot of images and has the Load More feature also.

I tried these measures.

In viewDidLoad() I am setting the cache size.

let cache = KingfisherManager.sharedManager.cache
cache.maxMemoryCost = 50 * 1024 * 1024
// Set max disk cache to 50 mb. Default is no limit.
cache.maxDiskCacheSize = 50 * 1024 * 1024
// Set max disk cache to duration to 3 days, Default is 1 week.
cache.maxCachePeriodInSecond = 60 * 60 * 24 * 3

In viewWillDisappear() I am clearing this.

cache.clearMemoryCache()
// Clear disk cache. 
cache.clearDiskCache()
// Clean expired or size exceeded disk cache.
cache.cleanExpiredDiskCache()

Still memory does not gets free as expected. Kindly correct me if I m missing something.

like image 463
Ankit Kumar Gupta Avatar asked Nov 23 '16 07:11

Ankit Kumar Gupta


2 Answers

On receiving memory warning I cleared the cache and it worked fine for me now :

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    cache.clearMemoryCache()
    cache.clearDiskCache()
    cache.cleanExpiredDiskCache()
}

Update for Swift 4 :

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    KingfisherManager.shared.cache.clearMemoryCache()
    KingfisherManager.shared.cache.clearDiskCache()
    KingfisherManager.shared.cache.cleanExpiredDiskCache()
}
like image 53
Ankit Kumar Gupta Avatar answered Oct 10 '22 16:10

Ankit Kumar Gupta


KingFisher in Swift 4

KingfisherManager.shared.cache.clearMemoryCache()
KingfisherManager.shared.cache.clearDiskCache()
KingfisherManager.shared.cache.cleanExpiredDiskCache()
like image 26
Chhaileng Avatar answered Oct 10 '22 14:10

Chhaileng