Van Du Tran Van Du Tran - 3 months ago 27
iOS Question

Difference between cache policy of NSURLSessionConfiguration and NSMutableURLRequest

I am using code:

let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.requestCachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
let session = NSURLSession(configuration: configuration)
let request = NSMutableURLRequest(URL: url)
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
request.HTTPMethod = "POST"
request.HTTPBody = bodyData
let dataTask = session.dataTaskWithRequest(request) { (data: NSData?, response: NSURLResponse?, error: NSError?) in
//...
}


I am wondering what's the difference between these two, and which one has to be used.

configuration.requestCachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData


Thanks.

Answer

I believe it's simply a matter of whether you want the no caching policy to apply to a single request or a whole family of requests from the time you set it.

Per this document:

https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSessionConfiguration_class/#//apple_ref/occ/instp/NSURLSessionConfiguration/requestCachePolicy

If you apply the policy to the configuration, then any NSURLSessionTask created within the session using that configuration will adopt that policy.

Otherwise, if you apply the policy to the request, that request, and that request only, will adopt the policy.

Comments