1

I would like to cache the data I am receiving from my API requests.

More precisely, I would like a 5 - 10 minutes maximum lifetime for my cache data.

Indeed, I am working on a weather app and I don't need database, but just to be able to reuse data from previous requests.

For example :

  • User requests forecast data for New York
  • User requests forecast data for London

Now if user requests New York again, I would like to reuse data from previous request. Weather data are updated every 15 minutes on the backend side (that's why 5 minutes cache is enough, navigation in app for a weather app doesn't last that much).

Here is what I have currently :

let configuration = URLSessionConfiguration.default
configuration.requestCachePolicy = .returnCacheDataElseLoad
configuration.timeoutIntervalForRequest = TimeInterval(60)
configuration.timeoutIntervalForResource = TimeInterval(60)

let session = URLSession(configuration: configuration)

The problem here is, if I request weather data for New York for example, and add the city in my favorite city. And then, if I never launch my App for 2 days, I would load the 2 days old cached data.

I would like to configure the expiration time of my URLSessionConfiguration.

NB: All my model objects come from Codable protocol and WebServices.

  • Possible duplicate of [How to set NSURLRequest cache expiration?](https://stackoverflow.com/questions/19855280/how-to-set-nsurlrequest-cache-expiration) – Kamran Nov 05 '18 at 10:45
  • My backend doesn't handle (sadly) the Cache-Control parameter available in the HTTP header response when requesting data. –  Nov 05 '18 at 13:52
  • Why don't you use URLSessionConfiguration.ephemeral ? – Vaibhav Parmar Dec 21 '18 at 08:37

0 Answers0