Avatar
Avatar
Iceman
@swift-main @swift-5.5.3 -warn-concurrency import Foundation @MainActor struct Button { var action: () -> () init(_ action: @escaping () -> ()) { self.action = action } func work() { let action = action DispatchQueue.global().async { action() } } } @MainActor struct MyView { var body: some Any { Button { f() } } func f() { print("Thread.isMainThread", Thread.isMainThread) print(Thread.current) } } Task { await MainActor.run { let view = MyView() let button = view.body as! Button button.work() } } RunLoop.main.run(until: Date().addingTimeInterval(0.3)) print("end") (edited)
Thread.isMainThread false <Thread: 0x00007fec20002130> endstderr:<stdin>:11:13: warning: cannot use let 'action' with a non-sendable type '() -> ()' from concurrently-executed code action() ^ (edited)