如何制作`nsviewController` a@mainactor`并实现nsmenuitemvalidation“ a@mainactor”
我有一个自定义nsviewController
实现nsmenuitemvalidation
协议的子类。这很好,直到我将视图控制器标记为@mainactor
。
我认为苹果将所有视图控制器都标记为主要演员已经隔离了,但也许只能在iOS上这样做。但是我认为标记我的子类是安全的。
一旦添加@MainActor
行,我就会获得警告>实例方法“ validatemenuitem”隔离到全局actor'mainactor'无法满足协议'nsmenuitemvalidation':
@MainActor
final class MyViewController: NSViewController, NSMenuItemValidation {
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
}
}
IS IS 的相应要求。标记自定义nsviewController
子类是@MainActor
是不是可以的吗?还是我应该如何处理此警告?
我想标记我的视图控制器,因为它需要与(明确)隔离到主要演员的其他类别,我想避免完全不是必需的异步边界,知道视图控制器将始终运行主线程。
I've a custom NSViewController
subclass that implements the NSMenuItemValidation
protocol. That works just fine, until I mark the view controller as @MainActor
.
I believed Apple marked all view controllers as main actor isolated already, but maybe it does so only on iOS. But I thought it would be safe to mark my subclass as such.
Once I add the @MainActor
line, I get the warning Instance method 'validateMenuItem' isolated to global actor 'MainActor' can not satisfy corresponding requirement from protocol 'NSMenuItemValidation'
:
@MainActor
final class MyViewController: NSViewController, NSMenuItemValidation {
func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
}
}
Is it not okay to mark a custom NSViewController
subclass as @MainActor
? Or how should I deal with this warning?
I want to mark my view controller, because it needs to co-operate with other classes that are (explicitly) isolated to the main actor and I want to avoid various async boundaries that are totally not necessary, knowing the view controller will always run on the main thread.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须标记带有非分离的函数。我假设这是因为NSMENUITEMVALIDATION协议不限于主队列。
非分离的func dastemenuitem(_ benuitem:nsmenuitem) - > bool {}
You must mark the function w/ nonisolated. I'm assuming this is because the NSMenuItemValidation protocol is not restricted to the main queue.
nonisolated func validateMenuItem(_ menuItem: NSMenuItem) -> Bool { }