2

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?

Abdul Hoque Nuri
  • 1,105
  • 1
  • 9
  • 18
matsbauer
  • 424
  • 3
  • 16
  • 1
    isn't that exactly the sense of a cache? It will be cleaned if there are memory issues? So if you don't want this, you have to save your images in another directory.... – Chris Dec 06 '19 at 04:04
  • 1
    It's better to find why your app getting crash or Terminated due to memory issue. If you are trying to store more than device available memory. Off-course OS will terminate if there is no available memory for your App. First of all solve your issue. – Abdul Hoque Nuri Dec 06 '19 at 05:42
  • hey @Chris - that's partially true, but isn't that the reason why kingfisher moves the memory cache to disk? Disk storage shouldn't be impacted by memory issues? – matsbauer Dec 06 '19 at 12:37
  • 1
    No, because disk storage is in a temporary directory and will be cleaned up as well – Chris Dec 06 '19 at 13:52
  • @Chris Thanks for the clarification, that is perfect to know! Is it possible to move the temporary directory to a more permanent directory? – matsbauer Dec 06 '19 at 13:59
  • 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. – Chris Dec 06 '19 at 14:29

2 Answers2

2

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.

Chris
  • 7,579
  • 3
  • 18
  • 38
0

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

Blazej SLEBODA
  • 8,936
  • 7
  • 53
  • 93