json - 清除缓存的 URL
问题描述
我有一个下面的代码,除了拉刷新之外,它工作正常。它返回 .json 的缓存版本。如果我使用不同的 URL 函数,它会即时重新加载新的 .json,但如果我想使用相同的 URL 执行拉取刷新,它会提供缓存版本。
谢谢
static func loadDataFromURL(url: URL,completion: @escaping (_ data: Data?, _ error: Error?) -> Void) {
let sessionConfig = URLSessionConfiguration.default
sessionConfig.allowsCellularAccess = true
sessionConfig.timeoutIntervalForRequest = 15
sessionConfig.timeoutIntervalForResource = 30
sessionConfig.httpMaximumConnectionsPerHost = 1
let session = URLSession(configuration: sessionConfig)
// Use URLSession to get data from an NSURL
let loadDataTask = session.dataTask(with: url) { data, response, error in
guard error == nil else {
completion(nil, error!)
if kDebugLog { print("API ERROR: \(error!)") }
return
}
guard let httpResponse = response as? HTTPURLResponse, 200...299 ~= httpResponse.statusCode else {
completion(nil, nil)
if kDebugLog { print("API: HTTP status code has unexpected value") }
return
}
guard let data = data else {
completion(nil, nil)
if kDebugLog { print("API: No data received") }
return
}
// Success, return data
completion(data, nil)
}
loadDataTask.resume()
}
解决方案
您应该使用 URLSessionConfiguration 对象设置 session.configuration,该对象根据您的需要配置缓存策略,我认为您没有在上面的代码中设置。
推荐阅读
- kubernetes - 如何使我的 Jaeger 可以远程访问?
- python-3.x - 无法在 Linux Mint 上安装 pgAdmin4
- python - Docxtpl:向文本添加标题级别
- smooch - 无法通过 v2 api 端点创建 webhook
- php - 在运行时禁用 Laravel 中的 Clockwork
- r - R 中 Shiny 应用程序中的图形布局;使布局更简洁
- node.js - 在 node.js json 文件中批量删除
- css - CSS Grid 具有固定数量的 100 个网格单元,但行数和列数灵活
- google-chrome - 在本地存储中存储数据是一种好习惯吗?
- c# - Visual Studio 警告 - 不需要前缀“S_”或“T_”