X

iOS 使用 NSURLConnection 的 Cache 注意事項 - 2018

iOS 使用 NSURLConnection 的 Cache 突然某一天開始,成效驚人,完全不會來問 ETag,也不來問 Server,直到過期為止,完全不來碰 Server,所以研究看看他是看哪個 Header、要怎麼不讓他 Cache 等等的資料。

iOS 使用 NSURLConnection 的 Cache 注意事項

NSURLConnection 主要看 "Cache-Control" and "Expires",若要不被 Cache:

  • Expires:設定為現在日期時間,範例格式:expires: Mon, 26 Feb 2018 01:50:04 GMT
  • Cache-Control: max-age=0
  • Apple 官方文件:Understanding Cache Access

下述摘錄自此篇:Preventing NSURLConnection Cache Issues

  • NSURLRequestUseProtocolCachePolicy: This is the default value and means that depending on what the protocol dictates, the best fitting policy will be used.
  • You can use the "Cache-Control" and "Expires" header values that the server returns to control how iOS will cache the data.
  • For example, if you don’t want it to cache at all, you can set the "Expires" date equal to the Date header value and "Cache-Control: max-age=0".

相關網頁

  • Technical Q&A QA1727: TLS Session Cache
    • This solution can better be achieved by adopting the NSURLSession API.
    • Using this framework, you can created two NSURLSessions, one for each desired connection.
    • Since NSURLSession maintains it's own TLS session cache, the second connection will avoid consulting the first cache, and will return the authentication challenge
  • HTTP/1.1: Header Field Definitions
Tsung: 對新奇的事物都很有興趣, 喜歡簡單的東西, 過簡單的生活.
Related Post