Question about @MainActor

When trying to call a method on a class marked as running on the MainActor from within a background DispatchQueue, I correctly get a compiler error, which goes away when calling it from DispatchQueu.main.async { … }. Also fine and expected.

But what is the mechanism that allows the compiler to diagnose this?

I just skimmed the API diffs for DispatchQueue, and so didn’t spot anything related to the MainActor.

Would something similar be implemented for third party libraries like RxSwift?

I'm sure I either read something about this on the forums or saw it in a WWDC session, but now can't find it. I took a look through the compiler source and found this:

Looks like DispatchQueue.main.async is special-cased to infer that it is @MainActor.

2 Likes

Excellent! Thanks a lot! :blush:

Terms of Service

Privacy Policy

Cookie Policy