I am annotating my function with @MainActor
to ensure that it can be called from any async location safely to trigger a UI Update. Despite, I run into an error where, somehow, the UI Update seems to be attempted on a background thread, even though (to my understanding) the function is strictly bound to the `@MainActor´.
This is my code:
/// Dismisses the popup from its presenting view controller.
@MainActor public func dismiss() {
presentingViewController?.dismiss(self)
}
It is called from inside an NSViewController
which listens to a certain event using NotificationCenter
, after which it initiates dismissal in the following objc
func:
class MainWindowControllerVC: NSWindowController, NSWindowDelegate {
override func windowDidLoad() {
NotificationCenter.default.addObserver(self, selector: #selector(self.dismissVCastSharePopup), name: .NOTIF_DISMISS_POPUP, object: nil)
}
@objc private func dismissPopup() {
// some other cleanup is happening here
popup?.dismiss()
}
}
I get the following error:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow drag regions should only be invalidated on the Main Thread!'
and the following warnings:
Can somebody please explain how this is possible? What am I misunderstanding here? If I wrap the same code into a DispatchQueue.main.async
or even Task { @MainActor () -> Void in ... }
I don't get this error. It is specifically tied to the annotation before the function.