Fogmeister
Fogmeister

Reputation: 77631

URLSession caching even after app restart

I've just come across something that has entirely changed my mental image of URLSession caching in iOS.

We were hitting an endpoint that only ever got hit once.

Restarting the app wouldn't hit the endpoint again.

Deleting the app would cause it to hit the endpoint again... but only once.

The header of the response contains...

Cache-Control public, max-age=1800

So it is down to caching. By manually telling the URLSession to ignore the cache it would hit the endpoint again.

In the docs it shows the caching policy and how it works as a workflow diagram.

https://developer.apple.com/documentation/foundation/nsurlrequestcachepolicy/nsurlrequestuseprotocolcachepolicy

enter image description here

But where is the cached data stored once the app is terminated? Surely the app and everything to do with it is removed from memory?

Upvotes: 0

Views: 1034

Answers (1)

Derek
Derek

Reputation: 1021

The URLSession is using URLCache for it's caching system. It's used for all network resources. You can access it directly or setting your own through URLSessionConfiguration. The underlying location of the URLCache is on the file system rather than in memory. There is a way to manage cache yourself though. Say, for instance, your response should be encrypted on the device. Slightly bad example, but you get the point. ;)

Here's an article how to manage cache programmatically if you are needing more control over caching.

Upvotes: 1

Related Questions