Avatar
import SwiftUI import Observation struct User: Sendable, Identifiable { var id: String var name: String // ... } struct UserListView: View { @State private var state: UserListViewState = .init() var body: some View { List(state.users) { user in NavigationLink { } label: { Text(user.name) } } .task { // MainActor // await state.load() let x: (Int) -> Void = state.foo } } } //func foo(self: UserListViewState, x: Int) { // //} //func load(self: UserListViewState) async { // //} @Observable final class UserListViewState { private(set) var users: [User] = [] func foo(x: Int) { // 呼び出し元と同じisolation domainで実行 } func load() async { // 呼び出し元と同じisolation domainで実行 do { users = try await UserRepository.fetchAllValues() } catch { // Error handling } } } enum UserRepository { static func fetchAllValues() async throws -> [User] { [] // TODO } }