【问题标题】:Objective-c clear cache NSCachedURLResponseObjective-c 清除缓存 NSCachedURLResponse
【发布时间】:2015-06-20 02:11:50
【问题描述】:

我运行此代码是因为我认为我遇到了缓存问题:

NSCachedURLResponse *response = [[NSURLCache sharedURLCache] cachedResponseForRequest:request];
    if(response){
        NSLog(@"Got Response");
    }else{
        NSLog(@"No Response");
    }

我的问题是如何清除请求 URL 的缓存?

【问题讨论】:

    标签: ios objective-c caching


    【解决方案1】:

    NSURLRequest 有一个 cachePolicy 属性,它指定请求的缓存行为。

    设置以下缓存策略NSURLRequestReloadIgnoringLocalCacheData,当发出如下示例的请求时,将从 url 而不是从缓存中加载数据。

    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
    

    NSURLRequestReloadIgnoringLocalCacheData

    指定 URL 加载的数据应该从 原始来源。不应使用现有的缓存数据来满足 URL 加载请求。

    https://developer.apple.com/library/prerelease/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/index.html#//apple_ref/c/tdef/NSURLRequestCachePolicy

    【讨论】:

      【解决方案2】:

      使用 removeCachedResponseForRequest:NSURLCache 类的方法。

      Documentation link

      【讨论】:

        猜你喜欢
        • 2018-06-28
        • 1970-01-01
        • 1970-01-01
        • 2011-01-03
        • 1970-01-01
        • 2011-07-25
        • 2017-04-06
        • 1970-01-01
        • 2016-04-09
        相关资源
        最近更新 更多