当我直接查询NSURLCache
时,我能够看到缓存的响应,但是当我通过NSURLSession:dataTaskWithRequest
请求相同的资源时,它总是查询服务器并且从不给我缓存的响应,即使禁用了互联网。
我在NSURLCache
中配置application:didFinishLaunchingWithOptions
,如下所示:
let URLCache = NSURLCache(memoryCapacity: 20 * 1024 * 1024,
diskCapacity: 80 * 1024 * 1024, diskPath: nil)
NSURLCache.setSharedURLCache(URLCache)
然后我使用此代码检查缓存并获取响应:
print("cached response is \(NSURLCache.sharedURLCache().cachedResponseForRequest(request)?.response)")
NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
print("nsurlsession response \(response)")
}.resume()
我的调试打印结果是:
cached response is Optional(<NSHTTPURLResponse: 0x7f809d0ddfe0> { URL: https://api.test.com/stuff } { status code: 200, headers {
"Cache-Control" = "public, s-maxage=600";
Connection = "keep-alive";
"Content-Type" = "application/json";
Date = "Tue, 06 Sep 2016 23:41:24 GMT";
Etag = "\"4800a836fee27c56a3cce1f0f2bddaefa5a7785b\"";
Server = "nginx/1.11.3";
"Transfer-Encoding" = Identity;
"X-RateLimit-Limit" = 60;
"X-RateLimit-Remaining" = 2; } })
nsurlsession response Optional(<NSHTTPURLResponse: 0x7f809d202e50> { URL: https://api.test.com/stuff } { status code: 200, headers {
"Cache-Control" = "public, s-maxage=600";
Connection = "keep-alive";
"Content-Type" = "application/json";
Date = "Tue, 06 Sep 2016 23:51:52 GMT";
Etag = "\"4800a836fee27c56a3cce1f0f2bddaefa5a7785b\"";
Server = "nginx/1.11.3";
"Transfer-Encoding" = Identity;
"X-RateLimit-Limit" = 60;
"X-RateLimit-Remaining" = 52; } })
如您所见,服务器的Cache-Control标头设置为允许缓存。我没有对我的请求做任何特别的事情,只是创建一个带有URL的默认NSURLRequest
。每次触发请求时,新响应都会被缓存,但在后续请求中永远不会检索它。
NSURLSession
是否有理由不使用NSURLCache
中存储的回复?我必须做些什么来告诉NSURLSession
实际查找缓存中的请求?
答案 0 :(得分:9)
我无法绝对肯定地告诉你为什么没有咨询缓存,但我可以列出最可能的原因:
我可能忘了其他几个人。话虽如此,如果我忘了它们,那可能意味着它们没有记录。
因此...
如果您确认上面列出的所有内容都按预期工作,请在bugreporter.apple.com上提交错误并提供足够的代码来重现问题,并尽可能包含数据包转储。