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!
Rather than using the
sharedSession
, you also can create your ownNSURLSession
using aNSURLSessionConfiguration
that specifies a default cache policy. So, define a property for your session:And then:
Then requests using that session will use that
requestCachePolicy
.I was getting cache image for same url .. so i have done this
}
The below code worked for me, the catch is setting URLCache to nil.
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:
That got a truly non-cached version of the file, which I needed for bandwidth estimation calculations.
You need to set the
cachePolicy
onNSURLRequest
, here's the documentation.Here's some insight as to how caching works in general.
You can read about the specific enums you can use for specifying the
cachePolicy
here in particular:For example, you would do:
Swift 3, Xcode 8