Await in async function isolated to @MainActor resumes on non-main thread - Bug?

Apologies if this is not the right category to ask this question, I believe it is more related to Swift Concurrency than the use of AppKit.

I'm trying to convert some code to use the new async features of macOS 12 AppKit. I understand that generally an await might suspend and resume on a different thread. However, I thought if the async function in which this await occurs has been isolated to the @MainActor, the continuation would always execute on the main thread. This is not the case with await NSOpenPanel.begin(), which resumes on Queue : com.apple.root.user-initiated-qos.cooperative (concurrent).

Do I need to explicitly schedule the code after the await to run on the MainActor again (e.g. using MainActor.run) and being able to read the result from the url property? That seems odd.

If I use the older-style completion handler API of NSOpenPanel.begin(), the handler is executed on the main thread.

Many thanks for any insight.

(Using latest macOS 12 Beta, Xcode 13 beta 5)

3 Likes

Your code is correct, and it is the beta 5 compiler that is wrong. This pull request fixed the issue you are seeing. The first Xcode it made it into is the recent Xcode 13 RC.

Doug

2 Likes

Thanks Doug, I'm somewhat relieved it's been only a compiler mistake.

Terms of Service

Privacy Policy

Cookie Policy