2015-09-26 94 views
1

出于某种原因,我无法利用alamofire提供的缓存机制,并强制清除缓存以便我可以ge新的json内容将由alamofire解析。无法使用alamofire获取新内容(无法刷新缓存内容)

我检查了控制头和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; 
} }) 

我怎样才能改变最大年龄和使用reinvalidate控制头部,使alamofire可以知道存在服务器上的新内容,并尝试刷新缓存,而不是加载旧的数据?

谢谢。

回答

1

您不能更改响应本身,但是您可以更改第二个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) 
    }