configuration for NSMutableURLRequest, that cachePolicy is always ReloadIgnoringLocalAndRemoteCacheData

275 Views Asked by At

In my project I'm working with NSMutableURLRequest. Sometimes there are mistakes because of caching.

So I did

    let mutableURLRequest = makeURLRequestFrom(url: url, httpMethod: "GET", httpHeaders: httpHeaders, parameters: parameters)
    mutableURLRequest.cachePolicy = .ReloadIgnoringLocalAndRemoteCacheData

This is working fine. Now I don't want to set the cachePolicy for every NSMutableURLRequest. Is there a possibility to set a standard config for the cachePolicy?

For UI-elements I can set configs in the Appdelegate for the whole project, too.

Like this:

UILabel.appearance().textColor =  UIColor.red()

Maybe there is a similar solution for the cachePolicy? (I can't find yet)

1

There are 1 best solutions below

1
On

If you're using NSURLSession, you can specify a per-session cache policy. For NSURLConnection, I think the best you could do would be to modify the shared NSURLCache object and set its in-memory and on-disk sizes to zero, though I can't guarantee that the OS will honor that.