NSURLCache不会清除iOS8中存储的响应 [英] NSURLCache does not clear stored responses in iOS8

查看:91
本文介绍了NSURLCache不会清除iOS8中存储的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我需要清除缓存并对URL进行新调用时调用的示例函数

Here is the sample function i call when i need to clear cache and make a new call to 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 (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中不起作用。

In iOS 6/7 the response is deleted successfully for the requestURL, but in iOS 8 it never gets deleted. I have searched but could not find reason why this should not work in iOS8.

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

Any help will be appreciated…..

推荐答案

NSURLCache 在iOS 8.0.x上被破坏 - 它根本不会清除缓存,所以它无限增长。请参见 http://blog.airsource.co.uk/2014 / 10/11 / nsurlcache-ios8-broken / 进行详细调查。缓存清除在8.1测试版中得到修复 - 但 removeCachedResponseForRequest: 不是

NSURLCache is broken on iOS 8.0.x - it never purges the cache at all, so it grows without limit. See http://blog.airsource.co.uk/2014/10/11/nsurlcache-ios8-broken/ for a detailed investigation. Cache purging is fixed in the 8.1 betas - but removeCachedResponseForRequest: is not.

removeCachedResponsesSinceDate: 似乎可以在iOS 8.0上运行 - 这是一个为8.0添加的API,但尚未将其发布到文档(它在API差异中)。我不清楚它对任何人有什么用 - 当然你通常想做的是在特定日期之前删除缓存的回复

removeCachedResponsesSinceDate: does appear to work on iOS 8.0 - an API that was added for 8.0, but hasn't made it to the docs yet (it is in the API diffs). I am unclear what use it is to anyone - surely what you normally want to do is remove cached responses before a particular date.

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

removeAllCachedResponses works as well - but that's a real sledgehammer solution.

这篇关于NSURLCache不会清除iOS8中存储的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆