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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…