I have a ViewModel
that is isolated with @MainActor
and I assume that every part of this ViewModel
class should run on the main actor (main thread), right?
So the question is, what about the async call of URLSession.shared.data
?
Does it also run on main thread
? Isn't that a bad approach?
@MainActor
class ViewModel {
@Published var name: String = ""
func fetchName() async {
guard let (data, _) = try? await URLSession.shared.data(from: URL(string: "http://....")!),
let response = try? JSONDecoder().decode(Response.self, from: data) else {
return
}
self.name = response.name
}
}