Portrait

I2H3

Notes from work as an app developer for iPhones, iPads and Macs.

RSS Icon GitHub Mastodon

Nonisolated Extension Functions

With strict concurrency checking enabled in an Xcode project, I encountered a warning while using URLSession.

After a short research I came across this Apple developer forums post which suggest an interesting pattern: extending framework types with nonisolated functions. Reimplementing existing functions with the nonisolated keyword allows calls outside their actor’s isolated domain. This might be an acceptable solution in some use cases where a call is known to not have any side effect on state.

extension URLSession {
    public nonisolated func getData(for url: URL) async throws -> (Data, URLResponse) {
        try await data(from: url)
    }
}