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
method in your delegate, and call the completionHandler with a nil argument.
URLSession(_:dataTask:willCacheResponse:completionHandler:)
A similar delegate call works if you're using NSURLConnection
; see this question for details.
Related Topics
Swift - How to Get Last Taken 3 Photos from Photo Library
Nsattributedstring Inserting a Bullet Point
How to Apply a Tintcolor to a Uiimage
Uiwebview Random Crash at [Uiviewanimationstate Release]: Message Sent to Deallocated Instance
Access the Instance of a Viewcontroller from Another in Swift
Xcode Tabbed Application - Adding New Tab View
Apns Notifications Not Reaching Devices Enrolled in Apple Mdm
Avplayer Stops Playing Video After Buffering
Change the Uitextview Text Direction
Facebooksdk and Bolts Conflicting Each Other (Duplicate Symbols) on Build
Stretch Background Image for Uibutton
Move a View When Scrolling in Uitableview
Don't Launch Simulator When Running Unittests
How to Downscale a Uiimage in iOS by the Data Size
Spacing Between Uitableviewcells
Facebook Sdk 3.0: How to Receive User's E-Mail
Removing Badge from iOS App Icon
Detect Hash Tags #, Mention Tags @, in iOS Like in Twitter App