How to Disable Caching from Nsurlsessiontask

How to disable caching from NSURLSessionTask

If your read the links from @runmad you can see in the flow chart that if the HEAD of the file is unchanged it will still used the cached version when you set the cachePolicy.

In Swift3 I had to do this to get it to work:

let config = URLSessionConfiguration.default
config.requestCachePolicy = .reloadIgnoringLocalCacheData
config.urlCache = nil

let session = URLSession(configuration: config)

That got a truly non-cached version of the file, which I needed for bandwidth estimation calculations.

Clear NSURLSession Cache

Ephemeral mode will not use any cache.

NSURLSessionConfiguration *ephemeralConfigObject = [NSURLSessionConfiguration ephemeralSessionConfiguration];

Prevent caching in NSURLSession for iOS Simulator

I had a similar issue and I was able to fix this by setting the properties- URLCache and requestCachePolicy on NSURLSessionConfiguration to nil and NSURLRequestReloadIgnoringCacheData respectively.

Also, you can try setting the cachePolicy property on NSMutableURLRequest to NSURLRequestReloadIgnoringCacheData.

Is it possible to prevent an NSURLRequest from caching data or remove cached data following a request?

Usually it's easier to create the request like this

NSURLRequest *request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:60.0];

Then create the connection

NSURLConnection *conn = [NSURLConnection connectionWithRequest:request
delegate:self];

and implement the connection:willCacheResponse: method on the delegate. Just returning nil should do it.

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
return nil;
}

Stop NSURLRequest writing to cache

NSURLRequest's cache policy only controls if and how the cache will be read, not whether downloaded data will be written.

If you're using NSURLRequest with NSURLSession, implement the
URLSession(_:dataTask:willCacheResponse:completionHandler:)
method in your delegate, and call the completionHandler with a nil argument.

A similar delegate call works if you're using NSURLConnection; see this question for details.



Related Topics



Leave a reply



Submit