簡體   English   中英

URLSession datatask正在訪問舊版本的網站?

[英]URLSession datatask accessing older version of website?

我正在使用URLSession從我的網站抓取JSON數據。 我的代碼拋出與轉換類型有關的各種錯誤,因此我在代碼中添加了一些打印語句,並發現此功能出於某種原因正在訪問我的網站的較早版本。 此后,我已經更新了網站的數據,並通過自己訪問網站和使用Rested驗證了新數據是否正確顯示。 但是,下面代碼中的打印語句會產生舊數據。 該代碼不會從磁盤讀取數據,因此我不確定為什么會這樣。

為了隱私起見,我已經從代碼中刪除了網站的鏈接,但是可以在下面找到該功能。

   func websiteToDisk() {

        let config = URLSessionConfiguration.default

        config.waitsForConnectivity = true

        let defaultSession = URLSession(configuration: config)

        let url = URL(string: someURL)

        let task = defaultSession.dataTask(with: url!) { data, response, error in

            do {

                print("Getting information from website")

                if let error = error {

                    print(error.localizedDescription)

                } else if let data = data,

                    let response = response as? HTTPURLResponse,

                    response.statusCode == 200 {

                    //do {

                    let jsonDecoder = JSONDecoder()

                    print("about to dcode")

                    let decodedData = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]//try jsonDecoder.decode([String: [String]].self, from: data)

                    print(decodedData)

                    print("accessing dictionary")

                    print(decodedData!["busLoops"])

                    let toWrite = decodedData!["busLoops"] as! [String]



                    let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

                    let busLoopsURL = documentDirectoryURL.appendingPathComponent("busLoops").appendingPathExtension("json")



                    let jsonEncoder = JSONEncoder()

                    let jsonData = try jsonEncoder.encode(toWrite)

                    try jsonData.write(to: busLoopsURL)

                    //} catch { print(error)}

                }

            }

            catch { print(error)}

        }

        task.resume()

    }

嘗試忽略本地緩存數據

guard let url = URL(string: "http://....") else{
    return
}
let request = NSMutableURLRequest(url: url)
request.cachePolicy = .reloadIgnoringCacheData
let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, resp, error) in

}
task.resume()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM