Last active
July 13, 2024 08:39
-
-
Save benigumocom/190dff4b0197236540df1dfd61bf1167 to your computer and use it in GitHub Desktop.
【Swift】URLSession で Passing argument of non-sendable type '(any URLSessionTaskDelegate)?' outside of main actor-isolated context may introduce data races 👉 https://android.benigumo.com/20240709/swift-concurrency/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct TestView: View { | |
@State private var text = "-" | |
private let url = URL(string: "https://wttr.in/?format=3")! | |
//private let url = URL(string: "https://httpbin.org/get")! | |
var body: some View { | |
Text(text) | |
.task { | |
// ! Passing argument of non-sendable type '(any URLSessionTaskDelegate)?' | |
// outside of main actor-isolated context may introduce data races | |
//let (data, _) = try! await URLSession.shared.data(from: url) | |
// ! Capture of 'self' with non-sendable type 'TestView' in 'async let' binding | |
// Consider making struct 'TestView' conform to the 'Sendable' protocol | |
//async let (dataEx, _) = try! await URLSession.shared.data(from: url) | |
//let data = await dataEx | |
// ! Passing argument of non-sendable type 'TestView' | |
// outside of main actor-isolated context may introduce data races | |
// Consider making struct 'TestView' conform to the 'Sendable' protocol | |
//let (data, _) = await dataEx(url: url) | |
// OK | |
//let (data, _) = await URLSession.shared.dataEx(url: url) | |
// OK | |
//let (data, _) = await url.dataEx() | |
// OK | |
//let (data, _) = await WeatherA.getData(url: url) | |
// OK | |
//let (data, _) = await WeatherA().getData(url: url) | |
// OK | |
//let (data, _) = await WeatherC.getData(url: url) | |
// OK | |
let (data, _) = await WeatherC().getData(url: url) | |
text = String(data: data, encoding: .utf8)! | |
} | |
} | |
// ! | |
nonisolated private func dataEx(url: URL) async -> (Data, URLResponse) { | |
try! await URLSession.shared.data(from: url) | |
} | |
} | |
// OK | |
extension URLSession { | |
func dataEx(url: URL) async -> (Data, URLResponse) { | |
try! await data(from: url) | |
} | |
} | |
// OK | |
extension URL { | |
func dataEx() async -> (Data, URLResponse) { | |
try! await URLSession.shared.data(from: self) | |
} | |
} | |
actor WeatherA { | |
// OK | |
static func getData(url: URL) async -> (Data, URLResponse) { | |
try! await URLSession.shared.data(from: url) | |
} | |
// OK | |
nonisolated func getData(url: URL) async -> (Data, URLResponse) { | |
try! await URLSession.shared.data(from: url) | |
} | |
} | |
final class WeatherC: Sendable { | |
//class WeatherC: @unchecked Sendable { | |
// OK | |
static func getData(url: URL) async -> (Data, URLResponse) { | |
try! await URLSession.shared.data(from: url) | |
} | |
// OK | |
nonisolated func getData(url: URL) async -> (Data, URLResponse) { | |
try! await URLSession.shared.data(from: url) | |
} | |
} | |
#Preview { | |
TestView() | |
.frame(width: 300, height: 300) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
【Swift】URLSession で Passing argument of non-sendable type '(any URLSessionTaskDelegate)?' outside of main actor-isolated context may introduce data races
👉 https://android.benigumo.com/20240709/swift-concurrency/