Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable caching from NSURLSessionTask

In my iOS app, I am using NSURLSessionTask to download json data to my app. I discovered that when I call the url directly from the browser, I get an up to date json and when it's called from within the app, I get an older version of the json.

Is this due to caching? How can I tell NSURLSessionTask to not use caching.

This is the call I use:

NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

Thanks!

like image 424
Van Du Tran Avatar asked Jun 20 '14 13:06

Van Du Tran


People also ask

How do I stop caching?

Here's how... When you're in Google Chrome, click on View, then select Developer, then Developer Tools. Alternatively, you can right click on a page in Chrome, then click Inspect. Click on the Network tab, then check the box to Disable cache.

How do I disable cache in API?

Just use the Cache-Control: no-cache header. Implement it as delegating-Handler and make sure your header is applied (with MS Owin Implementation hook up on OnSendingHeaders() .

How do I disable localhost cache?

To activate it you have to go to: More Tools > Developer tools > Network "tab" then click on Disable cache.

How do you stop a flask from caching?

To disable caching in Python Flask, we can set the response headers to disable cache. to create the add_header function that adds a few headers to the response after each request is done. We make it run after each request with the @app. after_request decorator.


4 Answers

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.

like image 190
Dave Haupert Avatar answered Sep 24 '22 06:09

Dave Haupert


Rather than using the sharedSession, you also can create your own NSURLSession using a NSURLSessionConfiguration that specifies a default cache policy. So, define a property for your session:

@property (nonatomic, strong) NSURLSession *session; 

And then:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; configuration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData; self.session = [NSURLSession sessionWithConfiguration:configuration]; 

Then requests using that session will use that requestCachePolicy.

like image 39
Rob Avatar answered Sep 25 '22 06:09

Rob


Swift 4 & 5

I know it's been a while, but in case it might help someone in the future.

You may also use .ephemeral configuration property of URLSession, which doesn't save any cookies and caches by default.

As documentation goes,

An ephemeral session configuration object is similar to a default session configuration, except that the corresponding session object doesn’t store caches, credential stores, or any session-related data to disk. Instead, session-related data is stored in RAM.

So, your code might look like this:

let configuration = URLSessionConfiguration.ephemeral
let session = URLSession(configuration: configuration)
like image 26
joliejuly Avatar answered Sep 22 '22 06:09

joliejuly


Swift 3, Xcode 8

extension UIImageView {
func donloadImage(fromUrl url: URL) {
    let request = URLRequest(url: url, cachePolicy: URLRequest.CachePolicy.reloadIgnoringLocalCacheData, timeoutInterval: 60.0)
    URLSession.shared.dataTask(with: request) { (data, response, error) in
        guard
            let httpURLResponse = response as? HTTPURLResponse, httpURLResponse.statusCode == 200,
            let mimeType = response?.mimeType, mimeType.hasPrefix("image"),
            let data = data, error == nil,
            let image = UIImage(data: data)
            else { return }
        DispatchQueue.main.async() { () -> Void in
            self.image = image
        }
    }.resume()
}
like image 36
Gurjit Singh Avatar answered Sep 22 '22 06:09

Gurjit Singh