iphone – NSURLCache没有缓存

iphone – NSURLCache没有缓存,第1张

概述所以我已经子类化NSURLCache并且每次调用load HTMLFromString时:它调用storeCachedRequest:forRequest:然后调用cachedResponseForRequest:.这就是我所拥有的: - (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request{ NS 所以我已经子类化NSURLCache并且每次调用load HTMLFromString时:它调用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假定它应该缓存此特定结果. 总结

以上是内存溢出为你收集整理的iphone – NSURLCache没有缓存全部内容,希望文章能够帮你解决iphone – NSURLCache没有缓存所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1084336.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存