内容简介:不是100%肯定,但我认为您需要使用NSURLConnection返回的NSURLResponse而不是创建自己的NSURLResponse.我怀疑如果您只是创建一个NSURLResponse,它没有为缓存设置任何标头,因此NSURLCache假定它应该缓存此特定结果.翻译自:https://stackoverflow.com/questions/8936488/nsurlcache-not-caching
FromString时:它调用storeCachedRequest:forRequest:然后调用cachedResponseForRequest:.这就是我所拥有的:
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
{
NSString *pathString = [[request URL] absoluteString];
NSCachedURLResponse * response = [super cachedResponseForRequest:request];
if (response != nil)
return response;
else {
NSString* myString= @"testing";
NSData* data=[myString dataUsingEncoding: NSASCIIStringEncoding ];
//
// Create the cacheable response
//
NSURLResponse *response =
[[[NSURLResponse alloc]
initWithURL:[request URL]
MIMEType:[self mimeTypeForPath:pathString]
expectedContentLength:[data length]
textEncodingName:nil]
autorelease];
NSCachedURLResponse * cachedResponse =
[[[NSCachedURLResponse alloc] initWithResponse:response data:data] autorelease];
[self storeCachedResponse:cachedResponse forRequest:request] ;
//NSLog(@"DEFAULT CACHE WITH URL %@", [[request URL] absoluteString]);
return [super cachedResponseForRequest:request];
}
return nil;
}
- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request
{
[super storeCachedResponse:cachedResponse forRequest:request];
NSLog(@"STORE CACHED RESPONSE WITH URL %@", [[request URL] absoluteString]);
}
问题是当我调用cachedResponseForRequest时:在保存之后,响应总是为零.为什么是这样?
我有:
NSURLRequest * req = [NSURLRequest requestWithURL:[NSURL URLWithString:self.imageSource_]];
NSCachedURLResponse * response = [[NSURLCache sharedURLCache] cachedResponseForRequest:req];
if (response == nil)
NSLog(@"RESPONSE IS NIL");
else {
NSString* theString = [[NSString alloc] initWithData:response.data encoding:NSASCIIStringEncoding];
NSLog(@"RESPONSE IS NOT NIL %@", theString);
}
并且它总是打印响应为零. url字符串与它时的字符串相同
storeCachedResponse.出于某种原因,它不是缓存.我已将缓存大小设置为一定的大小.
不是100%肯定,但我认为您需要使用NSURLConnection返回的NSURLResponse而不是创建自己的NSURLResponse.我怀疑如果您只是创建一个NSURLResponse,它没有为缓存设置任何标头,因此NSURLCache假定它应该缓存此特定结果.
翻译自:https://stackoverflow.com/questions/8936488/nsurlcache-not-caching
以上所述就是小编给大家介绍的《iphone – NSURLCache没有缓存》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
ActionScript 3.0 Cookbook中文版
Joey Lott、Adobe Dev Library / 陈建勋 / 电子工业 / 2007-11-01 / 78.00元
本书讨论了在Flash Player里执行的ActionScript3.0语言,采用问题—解法—讨论的形式讲解开发过程中常见问题的实际解法,例如:检测用户的 Flash Player 版本或操作系统;格式化日期和货币类型;接受用户输入及操作文字字符串;在运行时绘制各种形状;访问音频和视频;使用 Flash Remoting 进行远程过程调用;加载、发送和检索 XML 数据等。 全书涵盖客户端......一起来看看 《ActionScript 3.0 Cookbook中文版》 这本书的介绍吧!
MD5 加密
MD5 加密工具
RGB HSV 转换
RGB HSV 互转工具