iOS Swift - URLSessionDataTask doesn't get updated JSON data on refresh

Solution 1:

This is due to URLSession caching the data from the server, this is usually sent in the header of the json file coming from the server.

If you can't change anything on the server side then on you can just use the .ephemeral which apple documents here

use this code

let configuration = URLSessionConfiguration.ephemeral
let session = URLSession(configuration: configuration)

.ephemeral is used for data that will only be stored in ram and nothing will be stored on disk

Solution 2:

I can think of two ways.

1) add current date time at the end of your url

2) Disable Local Cache.

For 1)

extension Date {
    func currentTimeMillis() -> Int64 {
        return Int64(self.timeIntervalSince1970 * 1000)
    }
}

let url = URL(string: JSON_PATH + fileName + ".json?q=\(Date().currentTimeMillis())")

For 2)

let config = URLSessionConfiguration.default
config.requestCachePolicy = .reloadIgnoringLocalCacheData
config.urlCache = nil

let session = URLSession.init(configuration: config)

Taken From this SO