I'm using Alamofire's ImageCache but am running into some issues whereby images are being purged before I need to use them.
In ImageCache.swift
we find the following auto purging logic:
synchronizationQueue.async(flags: [.barrier]) {
if self.currentMemoryUsage > self.memoryCapacity {
let bytesToPurge = self.currentMemoryUsage - self.preferredMemoryUsageAfterPurge
var sortedImages = self.cachedImages.map { $1 }
sortedImages.sort {
let date1 = $0.lastAccessDate
let date2 = $1.lastAccessDate
return date1.timeIntervalSince(date2) < 0.0
}
var bytesPurged = UInt64(0)
for cachedImage in sortedImages {
self.cachedImages.removeValue(forKey: cachedImage.identifier)
bytesPurged += cachedImage.totalBytes
if bytesPurged >= bytesToPurge {
break
}
}
self.currentMemoryUsage -= bytesPurged
}
}
Essentially, when we hit the pre-defined memoryCapacity
value, the cachedImage
array is being sorted by its access date (oldest first) and images are then being deleted until the preferredMemoryUsageAfterPurge
value is met.
The default in Alamofire for memoryCapacity
is 100mb. In my app, I have a load of product images presented (and cached) in a tableView and when the user taps on any of them, they are taken to a product detail view where we need to use the image that has been cached for that given cell and product. Depending on the sizes of the images in the cached array and the scrolling activity of the user, I am sometimes finding that by the time the user taps on the required product, the image has already been purged.
So, I am thinking of changing the memoryCapacity
and wondered whether anyone has any experience with this and what might be a reasonable value to change it to. For example, has anyone manually amended this to e.g. 200 mb and had any performance issues as a result?