ios 如何禁用 AFNetworking 缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9968050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To Disable AFNetworking Cache
提问by Ashley Staggs
Is it possible to disable all the cache features from AFNetworking?
是否可以禁用 AFNetworking 的所有缓存功能?
I am building my own custom cache system and don't want this to take up disk space too.
我正在构建自己的自定义缓存系统,并且不希望它也占用磁盘空间。
Thanks, Ashley
谢谢,阿什利
回答by mattt
Cacheing is handled application-wide by NSURLCache
. If you don't set a shared cache, requests are not cached. Even with a shared NSURLCache
, the default implementation on iOS does not support disk cacheing anyway.
缓存在应用程序范围内由NSURLCache
. 如果您不设置共享缓存,则不会缓存请求。即使使用 shared NSURLCache
,iOS 上的默认实现也不支持磁盘缓存。
That said, unless you have a very particular reason to write your own cacheing system, I would stronglyrecommend against it. NSURLCache
is good enough for 99.9% of applications: it handles cache directives from incoming responses and uses them appropriately with new requests, and does so automatically in a way that is unlikely to be a performance bottleneck in your application. As someone who has wasted untold hours making one myself (and later throwing it away since it was completely unnecessary), I'd say that there are much better places to focus your development attention.
也就是说,除非您有非常特殊的理由来编写自己的缓存系统,否则我强烈建议您不要这样做。NSURLCache
对于 99.9% 的应用程序来说已经足够了:它处理来自传入响应的缓存指令,并在新请求中适当地使用它们,并且以不太可能成为应用程序性能瓶颈的方式自动执行此操作。作为一个已经浪费了无数时间自己制作一个(后来因为完全没有必要把它扔掉)的人,我想说有很多更好的地方可以集中你的开发注意力。
回答by addicted2oxygen
Initially I found Jason Moore's answer to work, however recently I have noticed my app is still caching requests. I am not using the latest AFNetworking, so I do not know if caching has been addressed in more recent builds.
最初我找到了 Jason Moore 的答案,但最近我注意到我的应用程序仍在缓存请求。我没有使用最新的 AFNetworking,所以我不知道在最近的版本中是否已经解决了缓存问题。
Apple's URLCache Project has this to say:
Apple 的 URLCache 项目是这样说的:
By default, the Cocoa URL loading system uses a small shared memory cache. We don't need this cache, so we set it to zero when the application launches.
默认情况下,Cocoa URL 加载系统使用一个小的共享内存缓存。我们不需要这个缓存,所以我们在应用程序启动时将它设置为零。
And then does this to disable the cache.
然后执行此操作以禁用缓存。
NSURLCache *sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:0
diskCapacity:0
diskPath:nil];
[NSURLCache setSharedURLCache:sharedCache];
[sharedCache release];
This will disable all caching for your whole app, which may not be ideal in some situations, but as NSURLRequest
is not honouring the requested cache policy, it is the only option left that I can see.
这将禁用整个应用程序的所有缓存,这在某些情况下可能并不理想,但由于NSURLRequest
不遵守请求的缓存策略,这是我能看到的唯一选项。
回答by Jason Moore
I've found on iOS 6 that requests are sometimes cached, even if the request has NSURLRequestReloadIgnoringCacheData
. Adding a cache response block that returns nil
prevents the request from being cached.
我发现在 iOS 6 上,请求有时会被缓存,即使请求有NSURLRequestReloadIgnoringCacheData
. 添加返回的缓存响应块nil
可防止请求被缓存。
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:20];
AFJSONRequestOperation *op =
[AFJSONRequestOperation JSONRequestOperationWithRequest:request];
// DISABLE CACHE //
[op setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) {
return nil;
}];
[op start];
回答by JakubKnejzlik
Unfortunately non of these methods worked for me. The only way I've managed to get the cache disabled is this way:
不幸的是,这些方法都不适合我。我设法禁用缓存的唯一方法是这样:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
回答by Yuri Brigance
This may depend on your server implementation, but adding Cache-Control:no-store
header to your outgoing request should result in a response that contains the same header, thus commanding NSURLCache
to not cache the response to disk.
这可能取决于您的服务器实现,但是向Cache-Control:no-store
您的传出请求添加标头应该会导致包含相同标头的响应,从而命令NSURLCache
不将响应缓存到磁盘。
IMHO this is a better approach than disabling disk-caching completely, especially if you're writing SDK code that will be used by other applications which may want to utilize NSURLCache
.
恕我直言,这是一种比完全禁用磁盘缓存更好的方法,特别是如果您正在编写 SDK 代码,而这些代码可能会被其他可能想要使用NSURLCache
.
回答by chings228
NSURLRequest *request = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];