swift - 无法使用 alamofire 获取新内容(无法刷新缓存内容)

标签 swift apache caching alamofire

出于某种原因,我无法利用 alamofire 提供的缓存机制,我被迫清除缓存,以便我可以获取新的 json 内容以供 alamofire 解析。

我检查了控件 header 和 etag,并打印了响应和请求,但我发现了一些奇怪的东西。检查最大年龄。我的网站使用 HTTP:

request <NSMutableURLRequest: 0x7a12b400> { URL: http://www.example.com/wp-json/posts }
response Optional(<NSHTTPURLResponse: 0x7a12fd30> { URL: http://www.example.com/wp-json/posts } { status code: 200, headers {
    Connection = "Keep-Alive";
    "Content-Encoding" = gzip;
    "Content-Type" = "application/json; charset=UTF-8";
    Date = "Sat, 26 Sep 2015 18:52:14 GMT";
    "Keep-Alive" = "timeout=5, max=100";
    "Last-Modified" = "Sat, 26 Sep 2015 17:43:31 GMT";
    Link = "</wp-json/posts?page=2>; rel=\"next\", 


    Server = Apache;
    **"Strict-Transport-Security" = "max-age=63072000; includeSubDomains";**
    "Transfer-Encoding" = Identity;
    Vary = "Accept-Encoding,User-Agent";
    "X-Content-Type-Options" = nosniff;
    "X-Frame-Options" = SAMEORIGIN;
    "X-Pingback" = "http://www.example.com/xmlrpc.php";
    "X-Powered-By" = "PHP/5.4.45";
    "X-WP-Total" = 11;
    "X-WP-TotalPages" = 2;
} })

如何更改 max-age 并使用 reinvalidate 控制 header ,以便 alamofire 可以知道服务器上有新内容并尝试刷新缓存而不是加载旧数据?

谢谢。

最佳答案

您无法更改响应本身,但您可以更改您发出的第二个 URL 请求以专门忽略缓存数据。

let URLRequest = NSMutableURLRequest(URL: NSURL(string: "https://httpbin.org/get")!)
URLRequest.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData

Alamofire.request(URLRequest)
    .responseJSON { request, response, result in
        print(request)
        print(response)
        print(result)
    }

关于swift - 无法使用 alamofire 获取新内容(无法刷新缓存内容),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32800911/

相关文章:

sql-server - sql server中的解析树缓存对象类型是什么

c# - Task.WhenAll() 一次只执行 2 个线程?

javascript - Nestjs如何控制缓存?

ios - NSManagedObject 和 Codable 用于存储在服务器和本地存储中的类

html - 来自 html 字符串的 iOS Swift 4 图像未在 WKWebView 中加载

ios - 终端获取URL后如何将数据传递到tableview? swift 5

php - Codeigniter 子域和 WAMP

ios - 与推送交互后导航到特定 ViewController

apache - RequestHeader 未在 htaccess 中的 RewriteRule 上设置

regex - mod_rewrite : Pass the path & query string URL as a parameter