如何从 NSURLSessionTask 禁用缓存 [英] How to disable caching from NSURLSessionTask

查看:28
本文介绍了如何从 NSURLSessionTask 禁用缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 iOS 应用程序中,我使用 NSURLSessionTask 将 json 数据下载到我的应用程序.我发现当我直接从浏览器调用 url 时,我会得到一个最新的 json,而当从应用程序中调用它时,我会得到一个旧版本的 json.

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.

这是因为缓存吗?如何告诉 NSURLSessionTask 不使用缓存.

Is this due to caching? How can I tell NSURLSessionTask to not use caching.

这是我使用的调用:

NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

谢谢!

推荐答案

如果你读了@runmad 的链接,你可以在流程图中看到,如果文件的 HEAD 没有改变,当你设置缓存策略.

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.

在 Swift3 中,我必须这样做才能让它工作:

In Swift3 I had to do this to get it to work:

let config = URLSessionConfiguration.default
config.requestCachePolicy = .reloadIgnoringLocalCacheData
config.urlCache = nil

let session = URLSession(configuration: config)

这得到了文件的真正非缓存版本,我需要它来进行带宽估计.

That got a truly non-cached version of the file, which I needed for bandwidth estimation calculations.

这篇关于如何从 NSURLSessionTask 禁用缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆