Understanding different cache policies when working with URLRequest in Swift
Learn how to manage cache when sending HTTP requests in your iOS applications.
31 Oct 2022 · 4 min read
When in comes to server communication in iOS, the system provides different caching strategies to improve performance and reduce network traffic.
By choosing a cache policy, we can decide whether the caching should depend on expiration dates or disabled entirely or whether the server should be contacted to determine if the content has changed since the last request.
Let's jump in and explore available URLRequest cache policies and their consequences.
Setting a cache policy for URL requests
let urlRequest = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData)
Alternatively, we can set the desired caching policy when configuring a URLSession. All requests created from this session inherit its cache policy.
let configuration = URLSessionConfiguration.defaultconfiguration.requestCachePolicy = .returnCacheDataElseLoadlet urlSession = URLSession(configuration: configuration)
The following caching policies are available:
Let's go through each policy and see how the cache is being managed.
The .useProtocolCachePolicy value is the default cache policy. It uses the caching logic defined in the protocol implementation.
When working with HTTP or HTTPS, the caching capabilities are built into the protocol so the server is able to communicate with the client on how the content should be cached.
For example, the server might send the following cache control headers:
Last-Modified: Mon, 08 Dec 2014 19:23:51 GMTETag: "adg2jl4kfmm5"Cache-Control: max-age=533280Expires: Sun, 03 May 2024 11:02:37 GMT
We don't need to dive deeper into what each header means, since the caching policy will do all the work for us. So depending on the values the server sends, the caching policy will basically perform the following behaviour:
- In case no cached response is available, the system fetches the data from the URL.
- In case a cached response is available, not expired and doesn't always require revalidation, the system returns the cached response.
- In case a cached response is available, but expired or requires revalidation, a HEAD request is send to the server to check if the resource changed. If it's changed, the system fetches the data from the URL. Otherwise, it returns the cached response.
The .reloadIgnoringLocalCacheData policy is self-explanatory, it simply fully ignores local cache data always sending the request to the server.
The .reloadIgnoringLocalAndRemoteCacheData also fully ignores local cache data. Additionally, it instructs proxies and other intermediates to ignore their caches so far as the protocol allows.
The .returnCacheDataElseLoad policy always uses cached data if available ignoring its age or expiration date. It sends the request to the server only if there is no cached data.
The .returnCacheDataDontLoad policy always uses cached data. If no cached data is available, it returns an error. So a request is never send to the server.
The .reloadRevalidatingCacheData policy uses cached data if it can be validated by the server. Otherwise, the request is send to the server.
Like to support my work?
Articles with related topics
How to change JSON keys by using Swift's CodingKey protocol
Learn how to map JSON data to a different structure in Swift.
27 Mar 2023 · 3 min read
How to use custom names for Swift properties when decoding JSON data
Learn to use Swift's CodingKey protocol.
20 Feb 2023 · 2 min read
Understanding date decoding strategies when working with JSON in Swift
Learn how to convert JSON dates to Swift's native Date type and vice versa.
14 Nov 2022 · 2 min read
GraphQL essentials you need to know as an iOS Developer
Master the networking concept behind GraphQL and their usage on iOS
12 Apr 2020 · 6 min read
Using URLSession with async/await in Swift
Learn how to make HTTP networking requests with URLSession's async/await API.
17 Oct 2022 · 3 min read
Latest articles and tips
Introduction to Kotlin for Swift developers - protocols, extensions & generics
Learn how protocols, extensions and generics in Kotlin work from a Swift developer's point of view.
02 Dec 2023 · 8 min read
How to store images in SwiftData
Learn how to efficiently store larger amounts of data in SwiftData.
27 Nov 2023 · 1 min read
How to migrate to a new schema with SwiftData in iOS
Learn how to create a migration plan in SwiftData.
20 Nov 2023 · 4 min read
How to implemet a free trial period for StoreKit 2 subscriptions in iOS
Learn how to implement StoreKit 2 introductory offers.
13 Nov 2023 · 2 min read
How to migrate from Localizable.strings to String Catalogs
Learn how to migrate your iOS project to .xcstrings for localization.
06 Nov 2023 · 1 min read