Skip to content

Bugfix: URLSessionConfiguration.requestCachePolicy was not taken into account#84

Merged
marcprux merged 1 commit intoskiptools:mainfrom
fhasse95:URLSession-Cache-Policy-Bugfix
Jan 11, 2026
Merged

Bugfix: URLSessionConfiguration.requestCachePolicy was not taken into account#84
marcprux merged 1 commit intoskiptools:mainfrom
fhasse95:URLSession-Cache-Policy-Bugfix

Conversation

@fhasse95
Copy link
Contributor

Currently, the requestCachePolicy of URLSessionConfiguration is not taken into account when performing HTTP requests since only the cachePolicy of the URLRequest itself is evaluated. This causes the session-level cache policy to be ignored whenever the request uses the default .useProtocolCachePolicy.

This PR introduces an effective cache policy that correctly mirrors Apple’s URLSession behavior:

  • If the URLRequest specifies an explicit cachePolicy, it takes precedence.
  • If the request uses .useProtocolCachePolicy, the session’s requestCachePolicy is applied instead.

Example Code:

import Foundation

func loadExchangeRates(currencyCode: String = "USD") {
    let urlString = "https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest" +
        "/v1/currencies/\(currencyCode.lowercased()).json"
    load(from: urlString) { result in
        switch result {
        case .success(let data):
            let jsonString = String(data: data, encoding: .utf8)!
            print(jsonString)
            
        case .failure:
            break
        }
    }
}

func load(
    from urlString: String,
    completionHandler: @escaping (Result<Data, Error>) -> Void) {
    
    if let url = URL(string: urlString) {
        let config = URLSessionConfiguration.default
        config.requestCachePolicy = .reloadIgnoringLocalCacheData
        
        let urlSession = URLSession(configuration: config)
            .dataTask(with: url) { (data, _, error) in
                if let error = error {
                    completionHandler(.failure(error))
                } else if let data = data {
                    completionHandler(.success(data))
                }
            }
        
        urlSession.resume()
    } else {
        completionHandler(.failure("Invalid URL".toError()))
    }
}

extension String {
    func toError() -> Error {
        return StringError(self)
    }
}

struct StringError: Error {
    let message: String
    init(_ message: String) {
        self.message = message
    }
}

Thank you for contributing to the Skip project! Please use this space to describe your change and add any labels (bug, enhancement, documentation, etc.) to help categorize your contribution.

Skip Pull Request Checklist:

  • REQUIRED: I have signed the Contributor Agreement
  • REQUIRED: I have tested my change locally with swift test
  • OPTIONAL: I have tested my change on an iOS simulator or device
  • OPTIONAL: I have tested my change on an Android emulator or device

@cla-bot cla-bot bot added the cla-signed label Jan 11, 2026
@marcprux
Copy link
Member

LGTM, thanks!

@marcprux marcprux merged commit df6258c into skiptools:main Jan 11, 2026
1 of 2 checks passed
@marcprux
Copy link
Member

Weird ... this is causing TestURLSession.testFinishTasksAndInvalidate to fail on iOS:

Test Case '-[SkipFoundationTests.TestURLSession testFinishTasksAndInvalidate]' started.
/Users/runner/work/skip-foundation/skip-foundation/Tests/SkipFoundationTests/Network/TestURLSession.swift:237: error: -[SkipFoundationTests.TestURLSession testFinishTasksAndInvalidate] : XCTAssertTrue failed
/Users/runner/work/skip-foundation/skip-foundation/Tests/SkipFoundationTests/Network/TestURLSession.swift:238: error: -[SkipFoundationTests.TestURLSession testFinishTasksAndInvalidate] : XCTAssertTrue failed
Test Case '-[SkipFoundationTests.TestURLSession testFinishTasksAndInvalidate]' failed (11.182 seconds).

How is that possible? SkipFoundation doesn't do anything on the iOS side…

@marcprux
Copy link
Member

Weird ... this is causing TestURLSession.testFinishTasksAndInvalidate to fail on iOS:

Nevermind. The tests pass now. I guess this was just an intermittent flaky test. Weird that it happened three times in a row, though, and that I had never seen it before…

@fhasse95 fhasse95 deleted the URLSession-Cache-Policy-Bugfix branch January 18, 2026 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants