NSURLCache是​​否持续启动?

我正在为iOS应用程序寻找一个networkingcaching解决scheme,这个解决scheme在整个启动阶段是持久的。 我开始阅读有关NSURLCache,但没有看到有关持久性的任何提及。 有没有人知道如何使用NSURLCache,然后closures并打开应用程序的行为? 它是否持续?

NSURLCache根据来自服务器的caching响应,cachingconfiguration和请求的caching策略,自动caching通过NSURLConnectionUIWebView进行请求。 这些响应在高速caching的生存期内存储在内存和磁盘上。


在旁边

我使用下面的代码validation了行为。 你不需要在你自己的代码中使用下面的任何一个。 这只是为了演示我如何确认行为。

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Prime the cache. [NSURLCache sharedURLCache]; sleep(1); // Again, this is for demonstration purposes only. I wouldn't do this in a real app. // Choose a long cached URL. NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://cdn.sstatic.net/stackoverflow/img/favicon.ico"]]; // Check the cache. NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:request]; NSLog(cachedResponse ? @"Cached response found!" : @"No cached response found."); // Load the file. [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL]; return YES; } 

代码执行以下操作:

  1. 将caching归档。 我注意到caching不会返回caching结果的行为,直到caching被初始化并有机会扫描磁盘。
  2. 创build一个长caching文件的请求。
  3. 检查URL是否存在响应并显示状态。
  4. 加载url。

在第一次加载时,您应该看到“没有findcaching响应”。 在后续运行中,您将看到“findcaching响应!”