NSURLConnectionDelegate的连接:willCacheResponse:随机调用

时间:2013-04-11 09:47:05

标签: ios nsurlconnection nsurlcache nsurlconnectiondelegate

以下是我的简单电话:

NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:500];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[connection start];

我尝试在谷歌上找到2个随机的pdf网址(搜索" truc文件类型:pdf"):

A) NSString *urlString = @"http://www.eatletruc.com/letruc.menu0411.pdf";
B) NSString *urlString = @"http://www.botruc.com/boat-specs/C-Truc-7.pdf";

它们都有类似的标题(在连接中使用allHeaderFields:didReceiveResponse :):

A)

"Accept-Ranges" = bytes;
Connection = "Keep-Alive";
"Content-Length" = 2641705;
"Content-Type" = "application/pdf";
Date = "Thu, 11 Apr 2013 08:53:39 GMT";
Etag = "\"19a7b55-284f29-4a0a5e94ae1a7\"";
"Keep-Alive" = "timeout=5, max=100";
"Last-Modified" = "Mon, 11 Apr 2011 15:05:50 GMT";
Server = Apache;

B)

"Accept-Ranges" = bytes;
Connection = "Keep-Alive";
"Content-Length" = 343793;
"Content-Type" = "application/pdf";
Date = "Thu, 11 Apr 2013 08:55:38 GMT";
Etag = "\"b6864a-53ef1-49400c1d95800\"";
"Keep-Alive" = "timeout=5, max=100";
"Last-Modified" = "Mon, 01 Nov 2010 17:01:20 GMT";
Server = "Apache/2.2.22 (Unix) mod_ssl/2.2.22 OpenSSL/1.0.0-fips mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635";

但是连接:willCacheResponse:只调用url B.我在Cache.db sqlite数据库中只找到了url B.

为什么没有缓存?

1 个答案:

答案 0 :(得分:3)

好的,问题来自文件的大小。

似乎NSURLCache不会缓存大于其磁盘容量5%的文件。

我的NSURLCache设置了50MB的磁盘容量,因此大于2.5MB的文件不会被缓存。

扩展磁盘容量解决了我的问题。

ps:您可以将磁盘容量扩展到最大2GB,因此缓存中的文件不能超过100MB。