swift - UseProtocolCachePolicy 隐藏 304 状态码

标签 swift caching nsurlcache

我在 url 请求上设置 cachePolicy。 第一次调用 url (GET) 返回状态码 200。 第二次调用相同的 url 返回 200。

如果使用休息客户端,我在第一次调用时获得 200,在第二次调用时,使用正确的 header (If-None-Match)304

在线阅读我了解到 iOS 序列化状态代码为 200 的第一个请求并为第二次调用检索它。

有一种方法可以在 UrlCache 中获取 304 而不是 200,或者了解数据是否来自缓存而不是网络。

最佳答案

来自 http://jonathanblog2000.blogspot.com/2017/07/ios-uiwebview-nsurlsession-cache.html :

If you want to test and verify the cache works properly in your app, you have to log the device http traffic, as the delegate of NSURLSession will never see the 304 status code returned from server, it will be automatically replaced by the status code in the cached response.

这是一件好事。这意味着您的应用程序可以假装您获得了成功的响应,而不用担心响应是如何获得的或发生了什么样的优化。无论是否从缓存中提取,您都可以以相同的方式处理响应。

关于swift - UseProtocolCachePolicy 隐藏 304 状态码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47355646/

相关文章:

ios - 使用带有 Firebase 后端的 UITableViewController 显示排行榜 (Swift 4.1)

ios - 想要显示透明的导航栏

javascript - 存储 Ajax 响应以显示给其他用户

ruby-on-rails-3 - rails3 缓存 : expire fragment with extension

spring boot 使用 redis 作为缓存,我可以使用 keyGenerator 但使用 key 失败

ios - iOS如何在不子类化的情况下将自定义代码添加到类方法中?

swift - SpriteKit 错误 - 更改字体颜色时

ios - RxSwift MVVM 实现查询

ios - iOS 9 及更高版本上的 URLCache.shared 行为

iphone - 扩展 NSURLCache 无法使用参数