有没有办法清除NSURLConnection缓存?
我用它来下载一些字符串但我仍然得到相同的字符串,即使我从我的服务器更改了它.
我有一个测试应用程序设置,即使用户在下载过程中切换应用程序,它也能成功从网络下载内容.太好了,现在我有后台下载了.现在我想添加缓存.没有必要让我多次下载图像,b/c系统设计,给定一个图像URL我可以告诉你该URL后面的内容永远不会改变.所以,现在我想使用Apple内置的内存/磁盘缓存来缓存下载的结果,我已经阅读了很多(而不是我在NSCachesDirectory中手动保存文件,然后在创建之前检查文件)请求,ick).在尝试得到缓存在此工作的代码顶部的工作,我添加以下代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Set app-wide shared cache (first number is megabyte value)
[NSURLCache setSharedURLCache:[[NSURLCache alloc] initWithMemoryCapacity:60 * 1024 * 1024
diskCapacity:200 * 1024 * 1024
diskPath:nil]];
return YES;
}
Run Code Online (Sandbox Code Playgroud)
当我创建会话时,我添加了两行NEW(URLCache和requestCachePolicy).
// Helper method to get a single session object
- (NSURLSession *)backgroundSession
{
static NSURLSession *session = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.example.apple-samplecode.SimpleBackgroundTransfer.BackgroundSession"];
configuration.URLCache = [NSURLCache sharedURLCache]; // NEW LINE …
Run Code Online (Sandbox Code Playgroud) iphone nsurlcache ios nsurlsession nsurlsessionconfiguration
我过去两年一直在使用RestKit,但最近我开始考虑从这些monolith框架过渡,因为它看起来真的有点过分.
这是我前进的优点:
I. 对象映射过程中RestKit的最大损失. 你能推荐你使用的独立库,它们表现出灵活和稳定吗?
II.我很伤心,我不需要功能齐全的存储,但我仍然需要在某些地方提供一些缓存支持.我听说NSURLCache在上一次操作系统发布中变得很有用.你有没有用它,策略是什么?当网络连接断开时,它是否返回缓存的API响应?
III.有人面临同样的问题吗?您应用了哪些解决方案?也许有人可以提供一些关于他或她在使用纯AFNetworking的多个应用程序中使用的架构的建议?
core-data nsurlcache restkit-0.20 afnetworking-2 nsurlsession
为什么它会缓存响应.它返回先前获取的响应.它甚至可以在关闭网络连接时工作.重置iOS模拟器似乎也不起作用.提出请求,然后再次使用Internet脱机工作(缓存).
public let urlSession: NSURLSession
public init()
{
// ...
var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.requestCachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
urlSession = NSURLSession(configuration: configuration)
}
func makeRequest(path: String, httpMethod:String, body:String?, baseUrl: String?, headers:[String:String]=[:], callback: JsonRequestCallback)
{
let urlString = (baseUrl ?? customOAuth2Manager.API_URL) + path
let url = NSURL(string: urlString)
let request = oauthInstance.request(forURL: url!)
request.HTTPMethod = httpMethod
self.customOAuth2Manager.setupRequest(request)
for (key, value) in headers {
request.setValue(value, forHTTPHeaderField:key)
}
if let body = body where body != "" {
let postData = (body as NSString).dataUsingEncoding(NSUTF8StringEncoding) …
Run Code Online (Sandbox Code Playgroud) caching nsurlcache nsurlsession nsurlsessionconfiguration swift
我正在寻找一个适用于我的iOS应用程序的网络缓存解决方案.我开始阅读有关NSURLCache的内容,但没有看到有关持久性的任何提及.当您使用NSURLCache然后关闭并打开应用程序时,是否有人知道这是如何表现的?它会坚持下去吗?
我在特定文件夹上设置了NSURLCache(../../Application Support/Offline,因此它不会被OS随机删除),然后我发送NSURLRequest和NSURLRequestReturnCacheDataElseLoad策略.
如何判断由符合NSURLConnectionDelegate的类处理的响应是来自缓存还是来自网络?
nsurlconnection nsurlrequest nsurlcache nsurlconnectiondelegate
这是我需要清除缓存并对URL进行新调用时调用的示例函数
- (void)clearDataFromNSURLCache:(NSString *)urlString
{
NSURL *requestUrl = [NSURL URLWithString:urlString];
NSURLRequest *dataUrlRequest = [NSURLRequest requestWithURL: requestUrl];
NSURLCache * cache =[NSURLCache sharedURLCache];
NSCachedURLResponse* cacheResponse =[cache cachedResponseForRequest:dataUrlRequest];
if (cacheResponse) {
NSString* dataStr = [NSString stringWithUTF8String:[[cacheResponse data] bytes]];
NSLog(@"data str r= %@",dataStr);
NSLog(@"url str r= %@",[[[cacheResponse response] URL] absoluteString]);
[cache storeCachedResponse:nil forRequest:dataUrlRequest];
[NSURLCache setSharedURLCache:cache];
}
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:dataUrlRequest];
//Check if the respnase data has been removed/deleted from cache
NSURLRequest *finalRequestUrlRequest = [NSURLRequest requestWithURL:requestUrl];
NSURLCache * finalCache =[NSURLCache sharedURLCache];
NSCachedURLResponse* finalcacheResponse =[finalCache cachedResponseForRequest:finalRequestUrlRequest];
if …
Run Code Online (Sandbox Code Playgroud) 这是我们在Swift 2中的代码.什么是Swift 3版本?我没有看到setShared的替代品.
let sharedCache: NSURLCache = NSURLCache(memoryCapacity: 0, diskCapacity: 0, diskPath: nil)
NSURLCache.setSharedURLCache(sharedCache)
Run Code Online (Sandbox Code Playgroud) 是否NSURLCache
透明地处理服务器收到的ETag?
我的意思是:它是否自动存储每个URL请求的ETag,然后在If-None-Match
提交相同URL的请求时发送适当的?或者我必须自己管理它?
我正在展示一个网络应用程序UIWebView
,有时页面内容会发生变化.更改内容后,应用程序将清除缓存.但是当我去一个我以前访问过的页面时,UIWebView
它不发送HTTP GET请求,但是从缓存加载,即使我已经禁用了缓存,如下所示:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
Run Code Online (Sandbox Code Playgroud)
最初我正在使用cachePolicy加载请求cachePolicy:NSURLRequestReturnCacheDataElseLoad
.
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:myURLString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0]];
Run Code Online (Sandbox Code Playgroud)
UIWebView
有某种内部缓存.已访问的页面将从此内部缓存加载而不是通过NSURLCache
,也没有发送请求.
有没有办法清除内部缓存UIWebView
?我甚至正在重新创建,UIWebView
但缓存仍在那里.
nsurlcache ×10
ios ×6
caching ×3
nsurlsession ×3
objective-c ×2
cocoa-touch ×1
core-data ×1
etag ×1
ios8 ×1
iphone ×1
nsurlrequest ×1
restkit-0.20 ×1
swift ×1
swift3 ×1
uiwebview ×1