如何使特定网址的iOS缓存无效? [英] How do I invalidate iOS's cache for a particular URL?
问题描述
使用NSURLSession
的默认缓存,如何使特定URL的缓存无效?
Using NSURLSession
's default caching, how do I invalidate the cache for a particular URL?
我注意到NSURLCache
的removeCachedResponseForRequest:
方法,但这需要一个NSURLRequest
对象,对于原始请求我没有.创建它们时是否需要存储它们,以便可以将它们传递回removeCachedResponseForRequest:
,还是可以仅创建一个具有适当URL的新URL,即使没有该URL,也可以用作该URL的等效项.标题字段和原始属性相同吗?
I note NSURLCache
's removeCachedResponseForRequest:
method, but that takes an NSURLRequest
object, which I don't have for the original request. Do I need to store those as I create them so I can then pass them back into removeCachedResponseForRequest:
or can I just create a new one with the appropriate URL which will then serve as equivalent for the purpose, even if it doesn't have the same header fields and other properties as the original?
推荐答案
该解决方案不是使现有URL的缓存无效,而是进行设置:
The solution turns out not to be invalidating the cache for an existing URL, but to set:
request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
下一次请求资源时,您知道该资源无效.有一些选项可以仅忽略本地缓存,也可以请求上游代理也忽略其缓存.参见 NSMutableURLRequest 文档了解详情.
When you make the next request for the resource you know to be invalid. There are options to ignore the local cache only, or to request that upstream proxies ignore their caches too. See the NSURLRequest/NSMutableURLRequest documentation for details.
这篇关于如何使特定网址的iOS缓存无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!