I have this issue in my app where if I background my app that had all its images loaded in and displayed, some of the cards in my UICollection view will have their images disappear after backgrounding the app for a couple of seconds and then foregrounding it again. I load my image in each cell using sd_setImage(with: URL(string: backgroundImageURL ?? "")). I thought the function sd_setImage was supposed to handle asynchronous downloading and caching of images so I am not sure why I have this problem. I also only have like 10-12 cards with medium sized preview images so it doesn't make sense as to why the cache would not be able store them and quickly load them in when the app is foregrounded.
I tried using the completion handler to see whether the images were being fetched from the network (not sure if I did this right), disk cache or memory cache. Turns out the images from the cards that first shows up without images are from the disk cache and the ones that have preloaded images are the memory cache.
I know memory cache is faster but the disk cache should not take so long that the user is not able to see the preview images for 6-7 seconds.
completed: { (image, error, cacheType, imageUrl) in
if let error = error {
print("Error loading image: \(error)")
return
}
// Check the cacheType to determine the source of the image
if cacheType == .none {
print("Image was fetched from the network")
} else if cacheType == .memory {
print("Image was loaded from memory cache")
} else if cacheType == .disk {
print("Image was loaded from disk cache")
}
}