New*_*Woo 2 nsurlcache ios nsurlsession
当我NSURLCache直接查询时,我能够看到缓存的响应,但是当我通过NSURLSession:dataTaskWithRequest它请求相同的资源时,它总是查询服务器并且永远不会给我缓存的响应,即使禁用了互联网.
我配置NSURLCache的application:didFinishLaunchingWithOptions是这样的:
let URLCache = NSURLCache(memoryCapacity: 20 * 1024 * 1024,
diskCapacity: 80 * 1024 * 1024, diskPath: nil)
NSURLCache.setSharedURLCache(URLCache)
Run Code Online (Sandbox Code Playgroud)
然后我使用此代码检查缓存并获取响应:
print("cached response is \(NSURLCache.sharedURLCache().cachedResponseForRequest(request)?.response)")
NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
print("nsurlsession response \(response)")
}.resume()
Run Code Online (Sandbox Code Playgroud)
我的调试打印结果是:
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; } })
Run Code Online (Sandbox Code Playgroud)
如您所见,服务器的Cache-Control标头设置为允许缓存.我没有对我的请求做任何特别的事情,只是NSURLRequest使用URL 创建默认值.每次触发请求时,新响应都会被缓存,但在后续请求中永远不会检索到.
是否有任何理由NSURLSession不使用存储的响应NSURLCache?有什么我必须要做的事情来NSURLSession实际查找缓存中的请求吗?
dga*_*ood 10
我不能绝对肯定地告诉你为什么没有咨询缓存,但我可以给你一个最可能的原因列表:
我可能忘了其他几个人.话虽如此,如果我忘了它们,那可能意味着它们没有记录.
所以...
如果您验证上面列出的所有内容都按预期工作,请在bugreporter.apple.com上提交一个错误,并包含足够的代码来重现问题,并尽可能包含数据包转储.