【问题标题】:NSURLCache does not clear stored responses in iOS8NSURLCache 不会清除 iOS8 中存储的响应
【发布时间】:2014-12-03 07:40:53
【问题描述】:

这是我需要清除缓存并重新调用 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 response data has been removed/deleted from cache
    NSURLRequest *finalRequestUrlRequest = [NSURLRequest requestWithURL:requestUrl];
    NSURLCache * finalCache =[NSURLCache sharedURLCache];

    NSCachedURLResponse* finalcacheResponse =[finalCache cachedResponseForRequest:finalRequestUrlRequest];

    if (finalcacheResponse) {
        //Should not enter here
        NSString* finaldataStr = [NSString stringWithUTF8String:[[finalcacheResponse data] bytes]];
        NSLog(@"data str r= %@",finaldataStr);
        NSLog(@"url  str r= %@",[[[cacheResponse response] URL] absoluteString]);
    }
}

在 iOS 6/7 中,requestURL 的响应已成功删除,但在 iOS 8 中,它永远不会被删除。 我已经搜索过,但找不到任何理由说明这在 iOS8 中不起作用。

任何帮助将不胜感激........

【问题讨论】:

    标签: ios objective-c caching ios8 nsurlcache


    【解决方案1】:

    NSURLCache 在 iOS 8.0.x 上已损坏 - 它根本不会清除缓存,因此它会无限增长。有关详细调查,请参阅http://blog.airsource.co.uk/2014/10/11/nsurlcache-ios8-broken/。缓存清除在 8.1 测试版中已修复 - 但removeCachedResponseForRequest: 不是

    removeCachedResponsesSinceDate: 确实 似乎可以在 iOS 8.0 上运行 - 为 8.0 添加的 API,但尚未进入文档(它在 API 差异中)。我不清楚它对任何人有什么用 - 当然您通常想要做的是在特定日期之前删除缓存的响应。

    removeAllCachedResponses 也可以 - 但这是一个真正的大锤解决方案。

    【讨论】:

    • 我还看到 removeCachedResponseForRequest: not working in iOS 8.3.
    • 在 9.3 和 9.3.1 中损坏
    • 在 11.2 中仍然存在问题 :( 不幸的是,这对我的项目依赖于操作缓存条目的“预取”功能至关重要。哦,好吧......
    • 在 iOS11 上:我正在尝试使用 storeCachedResponse 来解决我的问题:URLresponse is not retrieved after storing in cache using storeCachedResponse。但是 API 似乎不起作用。我的猜测是 both storeremove 都坏了。有什么想法吗?
    • 在 iOS 12.2 中仍然存在问题!
    【解决方案2】:

    重置特定 URL 的缓存响应后,我得到了足够的结果,将缓存控件更改为永远不会返回的内容,例如标题中的“max-age=0”。 Look here

    【讨论】:

      【解决方案3】:

      每当生成任何 API 调用时,该响应都会保存在缓存中。您可以在您的文档目录中找到该文件夹​​,创建一个名为 cachedb 的文件夹,其中包含响应列表。这可能是与安全相关的主要问题。使用任何第三方工具,某人都可以访问该信息。

      以下是我解决此问题的方法:

      NSURLCache *cache = [[NSURLCache alloc] initWithMemoryCapacity:0 * 1024 * 1024 diskCapacity:0 * 1024 * 1024 diskPath:nil]; [NSURLCache setSharedURLCache:cache];

      我已经为缓存分配了 0 个内存空间。因此缓存内存中不会存储任何数据。

      【讨论】:

      • 这是 iOS8 中的老问题....也许较新的版本不存在此问题...感谢您的回复...
      猜你喜欢
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-05
      • 2014-08-16
      • 2017-02-25
      • 2022-08-09
      • 2011-12-15
      相关资源
      最近更新 更多