【发布时间】:2014-01-28 04:35:27
【问题描述】:
我正在尝试从 NSURLCache 获取之前缓存的信息。使用此代码:
NSString *theCompleteURL = @"http://192.168.1.2:8080/api/endpoint.json";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:theCompleteURL]];
NSCachedURLResponse *response = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
if (response) {
NSLog(@"Cached data found");
}
else {
NSLog(@"Cached data not found");
}
但我总是得到 nil 作为方法 cachedResponseForRequest 中“响应”变量的响应。
我确信数据在缓存中,因为我在我的应用程序的 Cache.db 文件中检查了它,从 cfurl_cache_response 表中得到了这个结果:
sqlite> select * from cfurl_cache_response;
1|0|1875686237|0|http://192.168.1.2:8080/api/endpoint.json|2014-01-09 11:55:17|
sqlite>
在 ApplicationDelegate 中,NSURLCache 配置为:
NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:5 * 1024 * 1024
diskCapacity:40 * 1024 * 1024
diskPath:nil];
[NSURLCache setSharedURLCache:cache];
你知道缓存会发生什么吗?
我的端点的标头如下所示:
$ curl -I http://192.168.1.2:8080/api/endpoint.json
HTTP/1.1 200 OK
Content-Length: 1385
Expires: Thu, 01 Dec 2020 16
00: 00 GMT
Content-Type: application/json;charset=utf-8
ETag: "3e86-410-3596fbbc"
Cache-Control: max-age=3600
Connection: keep-alive
Server: thin 1.5.1 codename Straight Razor
$
【问题讨论】:
-
请发布响应标题。请贴出数据应该放在缓存中的代码
-
编辑添加标题
-
你能在哪里解决这个问题?
-
在 iOS 模拟器中重置内容和设置为我解决了这个问题。
标签: ios caching networking nsurlcache