Nonisolated Extension Functions
January 31, 2024 • #Concurrency #Swift
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)
}
}