NSURLConnection是否利用了NSURLCache? [英] Does NSURLConnection take advantage of NSURLCache?

查看:168
本文介绍了NSURLConnection是否利用了NSURLCache?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图弄清楚如何使用URL加载框架来加载利用缓存的URL。

I'm trying to figure out how to use the URL loading framework to load URLs taking advantage of caching.

我正在使用NSURLConnections并向它们提供NSURLRequests。我甚至在那些对NSURLRequestReturnCacheDataElseLoad的请求上设置了cachePolicy。第一次加载请求时,它会自动放入缓存中( [NSURLCache sharedCache] 有)。但是下次加载相同的请求时,NSURLConnection似乎忽略缓存中的内容并重新加载数据。

I am using NSURLConnections and feeding them NSURLRequests. I have even set the cachePolicy on those requests to NSURLRequestReturnCacheDataElseLoad. The first time I load a request, it does automatically get put in the cache ([NSURLCache sharedCache] has it). But the next time I load the same request, the NSURLConnection seems to ignore what's in the cache and reload the data.

我应该手动实现缓存查找并返回缓存数据? NSURLConnection不这样做吗?或者有没有办法让框架无缝地使用缓存?

Am I supposed to be manually implementing cache lookups and returning cached data? Does NSURLConnection not do this? Or is there some way to get the framework to use the cache seamlessly?

更新:尝试以下操作但没有成功:

UPDATE: Tried the following without success:


  • 将请求缓存策略设置为 NSURLRequestReturnCacheDataElseLoad 而不是 NSURLRequestUseProtocolCachePolicy

  • 重新使用请求对象而不是创建新的

  • 使用 + [NSURLConnection sendSynchronousRequest:returningResponse :错误:] 而不是异步加载

  • Setting the request cache policy to NSURLRequestReturnCacheDataElseLoad instead of NSURLRequestUseProtocolCachePolicy
  • Re-using the request object instead of making a new one
  • Using +[NSURLConnection sendSynchronousRequest:returningResponse:error:] instead of loading asynchronously

推荐答案

注意 iOS 5以后提供了一个共享URL,具有 内存和磁盘容量。

没什么除非您将NSURLCache设置为具有一些容量,否则将缓存:

Nothing will cache unless you set the NSURLCache to have some capacity:

// A 10MB cache. This a good avatar-image-cache size but might be too 
// large for your app's memory requirements. YMMV.
[[NSURLCache sharedURLCache] setMemoryCapacity:1024*1024*10];

默认的iPhone NSURLCache实例拒绝永久缓存到磁盘。如果您需要此行为,则必须对NSURLCache进行子类化并实现自己的磁盘缓存。我在GitHub上找到了很多磁盘缓存的例子,虽然它们都没有完全必要的修剪步骤令人满意地恕我直言。

The default iPhone NSURLCache instance refuses to ever cache to disk. if you need this behaviour you must sub-class NSURLCache and implement your own disk cache. I have found numerous examples of disk caches on GitHub, though none of them do the entirely necessary "prune" step satisfactorily IMHO.

这篇关于NSURLConnection是否利用了NSURLCache?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆