I ran into an interesting warning from Xcode 14 beta 3 when trying to fetch some data for a UIImageView
in a UIViewRepresentable
, whilst using targeted concurrency checking:
private struct MyView: UIViewRepresentable {
// ...
func updateUIView(_ uiView: View, context: Context) {
Task {
let (data, _) = try await URLSession.shared.data(from: imageURL)
uiView.imageView.image = UIImage(data: data)
}
}
}
It’s a pretty vanilla code snippet, but the call to URLSession.shared.data(from:delegate) generates this warning:
Non-sendable type '(any URLSessionTaskDelegate)?' exiting main actor-isolated context in call to non-isolated instance method 'data(from:delegate:)' cannot cross actor boundary
This makes sense in my head. But checking the documentation for URLSessionDataTask
claims that it is indeed Sendable. And Optionals provide Sendable conformance when their wrapped element also conforms.
So it must be the existential qualifier here that is “blocking” Sendable conformance. I couldn’t find any information on this after a quick search online, so I’m assuming the answer to this topic’s question is “no”.
But more concretely, how should we deal with this in the URLSession APIs? Does this imply that their definitions in Foundation need to change to account for this?