Search code examples
iosobjective-ccaching

Objective-c clear cache NSCachedURLResponse


I ran this code because I think I am running into a caching issue:

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

my question is how do I clear cache for the request URL ?


Solution

  • NSURLRequest has a cachePolicy property, which specifies the caching behaviour of the request.

    Set the following cache policy NSURLRequestReloadIgnoringLocalCacheData when making the request like the example bellow will load the data from the url and not from the cache.

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

    NSURLRequestReloadIgnoringLocalCacheData

    Specifies that the data for the URL load should be loaded from the originating source. No existing cache data should be used to satisfy a URL load request.

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