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)
swiftNightly BOT 12/24/2021 9:42 AM
Thread.isMainThread false <Thread: 0x00007fb86c002140> endstderr:<stdin>:11:13: warning: cannot use let 'action' with a non-sendable type '() -> ()' from concurrently-executed code action() ^ <stdin>:11:13: note: a function type must be marked '@Sendable' to conform to 'Sendable' action() ^ (edited)