Ral*_*alf 2 nsurlconnection nsurlcache ios
我们根据下面的缩写代码片段设置了一个简单的NSURLConnection和NSURLCache.我们已经确定服务器(localhost:9615)返回以下缓存头:
ETag : abcdefgh
Cache-Control : public, max-age=60
Run Code Online (Sandbox Code Playgroud)
但是,永远不会调用willCacheResponse委托方法.任何的想法?
码:
// In the app delegate
NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil];
[NSURLCache setSharedURLCache:URLCache];
// Triggered by a UIButton
- (IBAction)sendRequest:(UIButton *)sender {
NSLog(@"sendRequest");
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:9615"] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60.0];
NSMutableData *receivedData = [NSMutableData dataWithCapacity: 0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (!theConnection) {
receivedData = nil;
}
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse
{
NSLog(@"willCacheResponse");
// ...
}
Run Code Online (Sandbox Code Playgroud)
Rob*_*Rob 10
Cocoa应用各种标准来确定它是否可以缓存.例如,根据我的经验,willCacheResponse如果响应的大小超过持久存储高速缓存大小的大约5%,则不会看到被调用.我也看到其他人声称,如果max-age小于1835400,它也不会缓存(这不是我的经验,但也许旧的iOS版本受此影响).显然,要求必须是一个http或https要求,而不是ftp或file请求.
如果我的缓存足够大(并且我的响应正确提供Cache-Control并且Content-Type),我发现缓存正常工作.我只希望Apple明确表达所应用的标准(在文档或返回代码中),因为我和其他人浪费了几个小时来诊断缓存失败,只是意识到Cocoa正在应用一些秘密规则.
注意,如果NSURLConnection它本身通过从缓存中检索它而得到满足,willCacheResponse则不会被调用.当然,确保didFailWithError没有被召唤.
| 归档时间: |
|
| 查看次数: |
2134 次 |
| 最近记录: |