How to clear cached data stored by URLSession/URLConfiguration?
Asked Answered
A

1

7

I am using the code below to set the caching policy of my URLSession via URLConfiguration.

if appDelegateObj.configuration == nil {
            appDelegateObj.configuration = URLSessionConfiguration.default
   }

if self.apiType == ServerRequest.API_TYPES_NAME.API1  {


    if appDelegateObj.forceReload == true {
        appDelegateObj.configuration?.urlCache = nil
        appDelegateObj.configuration?.requestCachePolicy = .reloadIgnoringLocalCacheData

    }else{
        appDelegateObj.configuration?.requestCachePolicy = .returnCacheDataElseLoad
    }


}else{
    appDelegateObj.configuration?.requestCachePolicy = .reloadIgnoringLocalCacheData
}
session = URLSession(configuration: appDelegateObj.configuration!, delegate: nil, delegateQueue: nil)

The problem I am having is that I am getting the cached response back even after setting the

appDelegateObj.configuration?.urlCache = nil

The only way I am able to get fresh data is via using

appDelegateObj.configuration?.requestCachePolicy = .reloadIgnoringLocalCacheData

What am I doing wrong ? I need a way to clear all the cached data for the app.

I have tried using

URLCache.shared.removeAllCachedResponses()

but that too isn't working.

Antione answered 17/6, 2017 at 9:34 Comment(0)
K
8

Several points:

  • Setting the URL cache to nil does not disable all caching. A user can still get cached data from proxies.
  • Clearing the cache is not guaranteed to be instant. IIRC, it lags by several seconds.
  • Even if it were instant, the user could still get cached data from proxies.

In short, setting reloadIgnoringLocalCacheData is the correct way to do what you're doing (well, really, NSURLRequestReloadIgnoringLocalAndRemoteCacheData).

However, unless you're trying to support some weird offline mode, you probably do not want to be using returnCacheDataElseLoad. That mode ignores the cache expiration provided by the server, which you almost certainly do not want to do. Use the default policy (useProtocolCachePolicy) instead.

Kreiker answered 11/7, 2017 at 18:25 Comment(3)
Hi, so basically i can't control the data/resp that is once cached. I won't be able to clear the cache for a particular URL. Right ? I would need to change the policy in order to achieve/overcome the cached data for a particular URL ?Antione
You can clear it locally, but you can't guarantee that it will be immediately gone afterwards. It can take several seconds to propagate asynchronously to the cache on disk. And there's no way to clear any upstream caches (e.g. on proxy servers). The only thing you can do to avoid proxies caching the request is to use the "ignoring local and remote cache data" mode, which sends extra headers that tell (properly functioning) caches to ignore their cached data.Kreiker
Thanks for the clarification. I am surprised to know this fact. Really appreciate the help !Antione

© 2022 - 2024 — McMap. All rights reserved.