I am trying to understand as to why following piece of code throws an assertion. What I am trying to do is to call asyncFunc()
on main thread/main actor from call site. I don't want to decorate asyncFunc with @MainActor
as I want the function to be thread agnostic.
func asyncFunc() async -> String? {
dispatchPrecondition(condition: .onQueue(.main))
return "abc"
}
func callSite() {
Task { @MainActor in
await asyncFunc()
}
}
My understanding was that Task { @MainActor ...}
would execute all the following code on MainActor/main thread
.