【问题标题】:URLCache (iOS). storeCachedResponse works asynchronously. How to catch the completion?URL缓存(iOS)。 storeCachedResponse 异步工作。如何赶上完成?
【发布时间】:2018-12-05 02:52:37
【问题描述】:

刚刚发现函数 storeCachedResponse(_ cachedResponse: CachedURLResponse, for request: URLRequest) 是异步工作的。即执行后不会立即返回结果。我在官方文档中没有找到对此的描述。 见例子:

cache = URLCache(memoryCapacity: 0, diskCapacity: 100 * 1024 * 1024, diskPath: "myCache")
let config = URLSessionConfiguration.default
config.requestCachePolicy = .returnCacheDataElseLoad
config.urlCache = cache
let session = URLSession(configuration: config)
session.dataTask(with: request, completionHandler: {[unowned self]
    (data, response, error) in

    if let data = data, let response = response, ((response as HTTPURLResponse)?.statusCode ?? 500) < 300 {
        let cachedData = CachedURLResponse(response: response, data: data)                                            
        self.cache.storeCachedResponse(cachedData, for: request)
        let testCachedData = self.cache.cachedResponse(for: request)
    }
}

理论上 testCachedData 必须包含缓存的响应。但它实际包含的内容:

testCachedData?.response.url // Ok
testCachedData?.isEmpty // false
testCachedData?.data // 0 bytes!!!

虽然 testCachedData?.data 说它包含 0 个字节,但我们可以将这个数据写入一个文件,这个文件将包含真实数据,而不是 0。如果我们深入到本地缓存目录(~/Library/Caches/myApp/ MyCache)在 cachedResponse 调用后立即在断点处暂停时,我们可以看到带有缓存文件(fsCachedData)的文件夹还不存在。 现在让我们在 storeCachedResponse 和 cachedResponse 之间插入延迟:

cache = URLCache(memoryCapacity: 0, diskCapacity: 100 * 1024 * 1024, diskPath: "myCache")
let config = URLSessionConfiguration.default
config.requestCachePolicy = .returnCacheDataElseLoad
config.urlCache = cache
let session = URLSession(configuration: config)
session.dataTask(with: request, completionHandler: {[unowned self]
    (data, response, error) in

    if let data = data, let response = response, ((response as HTTPURLResponse)?.statusCode ?? 500) < 300 {
        let cachedData = CachedURLResponse(response: response, data: data)                                            
        self.cache.storeCachedResponse(cachedData, for: request)
        delay(5) // JUST 5 SEC DELAY
        let testCachedData = self.cache.cachedResponse(for: request)
    }
}

现在:

testCachedData?.response.url // Ok
testCachedData?.isEmpty // false
testCachedData?.data // contains bytes

因此,延迟 5 秒后,我们看到缓存文件夹 (fsCachedData) 存在并包含缓存文件(例如 D8A30D21-C8F1-4FCA-967E-F6B440998173)。

重点是如何捕捉storeCachedResponse的完成?

我将在创建缓存文件后立即使用它们。而且我要直接处理缓存文件,设置延迟不是最好的办法。

【问题讨论】:

  • 在上一行设置后没有意义?您已经在该函数中有响应,只需使用响应并在此函数之外使用任何您想使用的地方,然后从缓存中读取。
  • 你能更精确一点吗?你指的是哪个功能?函数还是闭包?
  • self.cache.storeCachedResponse(cachedData, for: request) delay(5) // 延迟 5 秒 let testCachedData = self.cache.cachedResponse(for: request)
  • 本主题的主题是响应的实例出现在数据实际缓存到文件之前。并且没有办法在它出现后立即使用响应。

标签: ios swift nsurlcache


【解决方案1】:

其实我不明白你为什么在缓存后立即调用缓存数据!?在我看来,如果数据存在,则应该在使用会话请求 url 之前调用缓存数据,否则返回缓存数据,否则从头开始请求。

例如:

private let allowedDiskSize = 100 * 1024 * 1024
private lazy var cache: URLCache = {
    return URLCache(memoryCapacity: 0, diskCapacity: allowedDiskSize, diskPath: "gifCache")
}()

typealias DownloadCompletionHandler = (Result<Data,Error>) -> ()

private func createAndRetrieveURLSession() -> URLSession {
    let sessionConfiguration = URLSessionConfiguration.default
    sessionConfiguration.requestCachePolicy = .returnCacheDataElseLoad
    sessionConfiguration.urlCache = cache
    return URLSession(configuration: sessionConfiguration)
}

private func downloadContent(fromUrlString: String, completionHandler: @escaping DownloadCompletionHandler) {

    guard let downloadUrl = URL(string: fromUrlString) else { return }
    let urlRequest = URLRequest(url: downloadUrl)
    // First try to fetching cached data if exist
    if let cachedData = self.cache.cachedResponse(for: urlRequest) {
        print("Cached data in bytes:", cachedData.data)
        completionHandler(.success(cachedData.data))

    } else {
        // No cached data, download content than cache the data
        createAndRetrieveURLSession().dataTask(with: urlRequest) { (data, response, error) in

            if let error = error {
                completionHandler(.failure(error))
            } else {

                let cachedData = CachedURLResponse(response: response!, data: data!)
                self.cache.storeCachedResponse(cachedData, for: urlRequest)

                completionHandler(.success(data!))
            }
        }.resume()
    }
}

及用法:

self.downloadContent(fromUrlString: ANY_URL, completionHandler: { (result) in

            switch result {
            case .success(let yourData):
                // handle data

            case .failure(let error):
                debugPrint(error.localizedDescription)
            }
 })

第一次它会从网络上获取数据,第二次请求它会立即返回缓存的数据。

【讨论】:

  • 很好的答案。谢谢你,编码员 ACJHP
  • 不客气,如果答案正确别忘了请采纳。
  • 一个很好的例子,说明 CacheURLResponse 和 storeCachedResponse 如何工作。谢谢@CoderACJHP。
  • 使用部分的目的是什么? @CoderACJHP
  • 仅举例
猜你喜欢
  • 2014-03-27
  • 1970-01-01
  • 2016-02-15
  • 2021-10-27
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 2019-01-21
  • 2013-02-05
相关资源
最近更新 更多