matsbauer
matsbauer

Reputation: 434

Swift App Termination removes Kingfisher Disk Cache

I'm having trouble with my kingfisher cache. The scenario: I save images from URL to a custom kingfisher cache.

When I read the given URL, it tells me that it first fetches the image from memory and after a few seconds, when refreshing, from disk. When I manually close the app and restart, on opening, kingfisher tells me that the cache disk size is for example 20MB. (This is what I expect)

Basically, the cache works perfectly all the time, except: When I move the app to the background it remains open for 5-30 minutes, until Apple/iOS kills it (Terminated due to signal 9) for memory reasons. When I start the app then, the cache (both memory and disk) is empty! How is that possible? What is the difference between the user and apple killing the application?

Upvotes: 2

Views: 1332

Answers (2)

Blazej SLEBODA
Blazej SLEBODA

Reputation: 9925

Both memory storage and disk storage have default expiration setting. Images in memory storage will expire after 5 minutes from last accessed (or will be removed during an application termination), while it is a week for images in disk storage. You can change this value by:

// Memory image expires after 10 minutes.
cache.memoryStorage.config.expiration = .seconds(600)

// Disk image never expires.
cache.diskStorage.config.expiration = .never

If you want to override this expiration for a certain image when caching it, pass in with an option:

// This image will never expire in memory cache.
imageView.kf.setImage(with: url, options: [.memoryCacheExpiration(.never)])

The expired memory cache will be purged with a duration of 2 minutes. If you want it happens more frequently:

// Check memory clean up every 30 seconds.
cache.memoryStorage.config.cleanInterval = 30

source: Kingfisher Cheat Sheet

Upvotes: 0

Chris
Chris

Reputation: 8091

yes, this is possible. you can give a directory url like this: ImageCache(name: "test", cacheDirectoryURL: url) -> url could be your documents folder e.g.

Upvotes: 2

Related Questions