gpt4 book ai didi

json - 如何在 Swift 中禁用缓存

转载 作者:行者123 更新时间:2023-11-28 07:37:17 25 4
gpt4 key购买 nike

在我的 iOS 应用程序中,我发现当我直接从浏览器调用 url 时,我会得到一个最新的 json,而当它从应用程序内部调用时,我会得到一个旧版本的 json。我已在加载 URL 的代码片段下方发布。

func getItems() {
//Hit the web service Url
let serviceUrl = "omitted"

//Download the json data
let url = URL(string: serviceUrl)
if let url = url{
//Create a URL Session
let session = URLSession(configuration: .default)
let task = session.dataTask(with: url, completionHandler: {(data, response, error) in
if error == nil {
//Succeeded
//Call the parse json function on the data
self.parseJson(data!)
}
else {
print("error occured in getItems")
}
})
// Start the task
task.resume()
}
}

最佳答案

您可以在URLRequest 中设置cachePolicy

您的代码将是

func getItems() {
//Hit the web service Url
let serviceUrl = "omitted"
let url = URL(string: serviceUrl)
//Download the json data
if let url = url{
//Create a URL Session
let session = URLSession(configuration: .default)
let request = URLRequest(url: url, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 15.0)
let task = session.dataTask(with: request, completionHandler: {(data, response, error) in
if error == nil {
//Succeeded
//Call the parse json function on the data
self.parseJson(data!)
}
else {
print("error occured in getItems")
}
})
// Start the task
task.resume()
}
}

关于json - 如何在 Swift 中禁用缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53055408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com