- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
所以我对 NSURLCache
进行了子类化,每次我调用 loadHTMLFromString:
它都会调用 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);
}
它总是打印response is nil。 url字符串和之前一样存储缓存响应
。由于某种原因,它不是缓存。我已将缓存大小设置为一定大小。
最佳答案
不是 100% 确定,但我认为您需要使用 NSURLConnection
返回的 NSURLResponse
而不是创建您自己的。我怀疑如果你只是创建一个 NSURLResponse
它没有为缓存设置任何 header ,所以 NSURLCache
假设它应该缓存这个特定的结果。
关于iphone - NSURLCache 不缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8936488/
我在将 URLResponse 缓存到磁盘时遇到问题。我想要这样,因为我正在下载的数据需要在满足 http header 中的过期标签时重新加载,而不是更早。另外,我希望 iPod touch 用户能
我正在编写一个 iPhone 应用程序,其中一个选项卡是 twitter feed,我正在解析 twitter xml 并将其很好地放入表格 View 中。如果没有互联网连接,我想显示上次有互联网连接
我在使用同步调用缓存 NSURLConnection 响应时遇到问题。我在一个类中初始化缓存,然后在另一个类中使用它。请注意高速缓存容量如何初始化为 100KB,但随后又神奇地重置为零。 - (id)
我在我的应用程序中使用 Alamofire 框架来处理 HTTP 请求。即使标准 URLCaching 未打开,Alamofire 也将其设置为默认值。 Caching Caching is hand
我对 this NSHipster article 有点困惑其中声明您必须在 appDidFinishLaunching 中为您的缓存初始化参数。我认为 NSURLCache 自动成为 NSURLSe
我正在尝试在服务器上存储大量图像并使用 AFNetworking 的图像缓存功能,该功能利用了 NSUrlCache,这样应用程序就不会不需要一直访问服务器,甚至可以离线工作。 重要的是图像永远不会过
我正在使用以下代码从 TfL 下载一些数据。 NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionC
Apple 从 iOS 5.0 开始支持磁盘缓存。我以前使用的是自制解决方案,但我正在测试 NSURLCache,希望最终能使用它,因为我在过去看到过奇怪的行为。 我遇到的一个更令人费解的问题是 ca
我正在使用 NSURLCache,我想为 NSURLRequest 设置自定义键。这可能吗? 完整解释: 我正在开发一个 map 应用程序,它使用 OpenStreetMap 瓦片。 OpenStre
我知道 NSCache 是线程安全的,但是我找不到任何文档提到 NSURLCache 是线程安全的。 最佳答案 除非documentation明确地说 NSURLCache 是线程安全的,那么它就不是
我正在尝试保护存储在 NSURLCache 中的敏感数据。我的应用程序文件和核心数据 sqlite 文件设置为 NSFileProtectionComplete。但是,我无法将 NSURLCache
我正在实现一个应用程序,它对我们也控制的 rest-api 进行大量网络调用。最近我们决定在服务器端引入缓存 header 以节省一些宝贵的网络和服务器时间。由于我们事先不知道数据的有效期,我们没有发
所以我对 NSURLCache 进行了子类化,每次我调用 loadHTMLFromString: 它都会调用 storeCachedRequest:forRequest: 然后是 cachedResp
我正在为我的 iOS 应用程序寻找一个跨启动持久的网络缓存解决方案。我开始阅读有关 NSURLCache 的内容,但没有看到任何关于持久性的内容。有谁知道当您使用 NSURLCache 然后关闭并打开
我有一个 NSURLCache,它只记录 cachedResponseForRequest 和 storeCachedResponse 调用。 似乎 UIWebView 会为每个请求调用 cached
我正在尝试弄清楚如何使用 URL 加载框架来利用缓存来加载 URL。 我正在使用 NSURLConnections 并向它们提供 NSURLRequests。我什至将这些请求的缓存策略设置为 NSUR
我正在使用 Cordova/Phonegap 构建一个 iOS 应用程序,它基本上有一个 UIWebView 并从外部资源加载我的网络应用程序。 (iOS6+) 此 Web 应用程序需要 450kb
我正在使用 AFNetworking 和 NSURLCache。请求是使用 NSURLRequestUseProtocolCachePolicy 创建的。应用程序接收带有 header 的响应: Et
This post建议这样做: - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application { [[NSUR
我有一个应用程序,我正在下载很多图像以供稍后显示。 即使没有互联网连接,该应用程序也需要正常运行,因此图像会一次性加载并使用 NSURLCache 持久化。 这是一个巧妙的解决方案,因为我可以使用普通
我是一名优秀的程序员,十分优秀!