0

I have got a problem in SWIFT 3, trying to disable cache for the requests, server sends updated JSON texts, but my application still shows old data. This happens only when cellular data is on, with WIFI everything works. Please, advise how to fix that, here is my code. thanks!

    let tim: String = String(Date().timeIntervalSinceReferenceDate)
    let urlTim = url + "?timref=" + tim


    URLCache.shared.removeAllCachedResponses()
    URLCache.shared.diskCapacity = 0
    URLCache.shared.memoryCapacity = 0
    if let cookies = HTTPCookieStorage.shared.cookies {
        for cookie in cookies {
            HTTPCookieStorage.shared.deleteCookie(cookie)
        }
    }

    var request = URLRequest(url: URL(string: urlTim)!, cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 10)

    request.httpMethod = "POST"
    request.httpBody = post.data(using: .utf8)
    request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringLocalAndRemoteCacheData

    let task = URLSession.shared.dataTask(with: request)
    {
        data, response, error in guard let data = data, error == nil else { print("Network Error"); err?(); return; }
        if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { print("Network 200 error"); err?(); return; }
        self.jsonResponse = String(data: data, encoding: .utf8)!;
    }
    task.resume()

1 Answers1

0

So here is my solution:

If anybody faces this situation and forgot to add cachepolicy option then one have to manually clean cache. After the code will work.