Instance Method インスタンスメソッド

urlSession(_:dataTask:willCacheResponse:completionHandler:)

Asks the delegate whether the data (or upload) task should store the response in the cache. データ(またはアップロード)タスクが応答をキャッシュに格納すべきかどうかを委任先に尋ねます。

Declaration 宣言

optional func urlSession(_ session: URLSession, 
                dataTask: URLSessionDataTask, 
       willCacheResponse proposedResponse: CachedURLResponse, 
       completionHandler: @escaping (CachedURLResponse?) -> Void)

Parameters パラメータ

session

The session containing the data (or upload) task. データ(またはアップロード)タスクを含んでいるセッション。

dataTask

The data (or upload) task. データ(またはアップロード)タスク。

proposedResponse

The default caching behavior. This behavior is determined based on the current caching policy and the values of certain received headers, such as the Pragma and Cache-Control headers. 省略時のキャッシュ挙動。この挙動は、現在のキャッシュ方針そして特定の受け取ったヘッダ、たとえばPragmaおよびCache-Controlヘッダの値に基づき決定されます。

completionHandler

A block that your handler must call, providing either the original proposed response, a modified version of that response, or NULL to prevent caching the response. If your delegate implements this method, it must call this completion handler; otherwise, your app leaks memory. あるブロック、それはあなたのハンドラが、元の提案された応答、その応答の修正されたバージョンのどちらか、またはNULLを応答のキャッシュを防ぐために提供して、呼び出さなければならないものです。あなたの委任先がこのメソッドを実装するならば、それはこの完了ハンドラを呼び出す必要があります;そうでなければ、あなたのアプリはメモリを漏洩します。

Discussion 議論

The session calls this delegate method after the task finishes receiving all of the expected data. If you don’t implement this method, the default behavior is to use the caching policy specified in the session’s configuration object. The primary purpose of this method is to prevent caching of specific URLs or to modify the userInfo dictionary associated with the URL response. セッションは、この委任先メソッドをタスクが、予想されるデータの全てを受け取ることを完了する後に呼び出します。あなたがこのメソッドを実装しないならば、省略時の挙動は、セッションのもつ構成設定オブジェクトにおいて指定されるキャッシュ方針を使うことになります。このメソッドの主たる目的は、特定のURLのキャッシュ化を防止すること、またはURL応答と結びつけられるuserInfo辞書を修正することになります。

This method is called only if the URLProtocol handling the request decides to cache the response. As a rule, responses are cached only when all of the following are true: このメソッドは、URLProtocolのリクエスト取り扱いが応答をキャッシュすることを決定する場合にのみ呼び出されます。通例は、応答がキャッシュされるのは、以下の全てが真である場合だけです:

  • The request is for an HTTP or HTTPS URL (or your own custom networking protocol that supports caching). リクエストが、HTTPまたはHTTPS URLのためのものである(またはキャッシュをサポートするあなた独自のあつらえのネットワークプロトコル)。

  • The request was successful (with a status code in the 200–299 range). リクエストが、成功だった(200–299の範囲の中の状態コードを持つ)。

  • The provided response came from the server, rather than out of the cache. 提供された応答が、サーバーからやって来た、キャッシュから出たのではなく。

  • The session configuration’s cache policy allows caching. セッション構成設定のもつキャッシュ方針が、キャッシュすることを許可する。

  • The provided URLRequest object's cache policy (if applicable) allows caching. 提供されたURLRequestオブジェクトのもつキャッシュ方針が(該当するならば)キャッシュすることを許可する。

  • The cache-related headers in the server’s response (if present) allow caching. サーバーの持つ応答の中のキャッシュ関連ヘッダ(もしあれば)が、キャッシュを許可する。

  • The response size is small enough to reasonably fit within the cache. (For example, if you provide a disk cache, the response must be no larger than about 5% of the disk cache size.) 応答サイズが、キャッシュ内に無理なく収まるほどに十分に小さい。(例えば、あなたがあるディスクキャッシュを提供するならば、応答はディスクキャッシュサイズのだいたい5%より大きくてはいけません。)