0
我正在使用下面的代码通过URLConfiguration来设置我的URLSession的缓存策略。如何清除URLSession/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)
我遇到的问题是,我甚至设置
appDelegateObj.configuration?.urlCache = nil
我能够得到新鲜的数据的唯一方法后,通过使用
appDelegateObj.configuration?.requestCachePolicy = .reloadIgnoringLocalCacheData
获取缓存的响应背面
我做错了什么?我需要一种方法来清除应用程序的所有缓存数据。
我一直在使用
URLCache.shared.removeAllCachedResponses()
尝试,但同样是行不通的。
嗨,所以基本上我无法控制一次缓存的数据/ resp。我无法清除特定网址的缓存。对 ?我需要更改策略才能达到/克服特定URL的缓存数据? –
您可以在本地清除它,但不能保证它会在以后立即被清除。可能需要几秒钟才能异步传播到磁盘上的缓存。并且无法清除任何上游缓存(例如在代理服务器上)。避免代理缓存请求的唯一方法是使用“忽略本地和远程缓存数据”模式,该模式会发送额外的头文件来告诉(正常运行的)缓存忽略缓存的数据。 – dgatwood
感谢您的澄清。我很惊讶地知道这个事实。真的很感谢帮助! –