Sometimes, it has to be fresh, be it bread, fish, or data.
But, sometimes, what you have got is what you need. And you don't need up-to-the-minute refreshes of data that you have already obtained. Cocoa offers much flexibility when it comes to caching request responses, and here's the easiest way to do it:
func cacheDataRequest()
{
let urlString = "http://www.grimshaw.de/Mac OS-book/test.json"
let url = URL(string: urlString)!
let request = URLRequest(url: url,
cachePolicy: .returnCacheDataElseLoad,
timeoutInterval: 20)
let session = URLSession.shared
let task = session.dataTask(with: request,
completionHandler:
{
data, response, error in
do {
let jsonData = try JSONSerialization.
jsonObject(with: data!,
...