When exactly do things get removed from urlcache's memory and disk?
Asked Answered
P

1

6
let memoryCapacity = 200 * 1024 * 1024
let diskCapacity = 1 * 1024 * 1024
let cache = URLCache(memoryCapacity: memoryCapacity, diskCapacity: diskCapacity, diskPath: "myDataPath")
URLCache.shared = cache

Scenario1

I'm setting urlcache's memory to 200mb and setting the disk space to 1mb. Then I download an image. Turn off internet and force quit the app, launch the app again by tapping it, and trigger a call (to download image) but it immediately crashes because data is nil

Needless to say if I turn off internet, it will still read from cache as expected and no crash would happen.

Scenario2

If I set the diskCapacity to 200mb (let diskCapacity = 200 * 1024 * 1024) then even though the cache is flushed out of memory, still the image is persisted in the disk; it will always show!


Based on this observation, I have the following questions:

  • Is it correct to say, every time you force quit or your app crashes due to a memory warning/issue your cache will get flushed, though it leaves your disk intact?
  • Any other reason where cache can get flushed out of memory?
  • When can things stored in disk get removed?
Permalloy answered 8/9, 2017 at 15:52 Comment(0)
F
3

Short answer:

The memory cache is lost when the app terminates (or, likely, under memory pressure, too). The disk cache persists across multiple invocations of the app, though can be removed if the device runs out of persistent storage and the OS reclaims space from caches and temp folders.


Long answer:

You ask:

Is it correct to say, every time you force quit or your app crashes due to a memory warning/issue your cache will get flushed, though it leaves your disk intact?

Largely. It may be more precise to say simply that all memory related to your app is discarded when the app terminates and that only those items saved to persistent storage can be retrieved when the app restarts.

Any other place where cache can get flushed out of memory?

You lose everything in the memory cache when the app terminates. There are obviously a few other situations in which case items can be removed:

  • If you manually remove responses from the URLCache.
  • Older, individual items are removed from the cache as you approach the max capacity of the cache and you try to add new items, forcing older items out.
  • Network responses often include a cache policy (indicating how long the response can be safely cached), so it's likely that they're removed at that point.
  • The memory cache may reasonably be purged upon memory pressure (e.g. .UIApplicationDidReceiveMemoryWarning).

When can things stored in disk get removed?

The logic is largely the same as prior point, except that (a) it can survive across invocations of the app); and (b) it's not flushed upon memory pressure, though it can be removed when the device runs low on persistent storage.

Fellini answered 8/9, 2017 at 16:23 Comment(5)
Thank you so much. Is there no callback for "when the device runs low on persistent storage"?Permalloy
Not that I know of.Fellini
The disk cache persists across multiple invocations of the app You mean if app is put into suspended state and then user taps on app icon, then they will still be using the cache. However if app was terminated then cache is all gone — regardless of reason (user -termination, app crash, memory warning which has ld to app crash or being kicked off memory when it was suspended)Permalloy
No, the “disk” cache (or, more accurately, the persistent storage) is preserved across terminations of the app. Now, some common third party image caching libraries purge the persistent cache, but that’s an implementation detail in those libraries. But the persistent storage is, itself, “persistent”.Fellini
My bad. I got myself confused with the word 'cache' there. Makes sense. disk cache == persistent storage != memory cache. I opened a new question here for some follow ups. Would you be able to take a look.Permalloy

© 2022 - 2024 — McMap. All rights reserved.