我有以下 SwiftUI Observable 模型类以及本地和全局参与者变量,但无法构建并出现错误Global actor 'MyGlobalActor'-isolated default value in a nonisolated context
。我的问题是全局参与者构建失败但本地参与者构建成功有什么特别之处?本地参与者中的 init 方法是否在默认上下文中运行,而全局参与者在隔离上下文中运行?
import SwiftUI
@Observable
final class Model {
let testActor = TestLocalActor() //This builds
let testGlobalActor = TestGlobalActor() //Build fails here
}
actor TestLocalActor {
init() {
}
}
@MyGlobalActor
final class TestGlobalActor {
init() {
}
}
@globalActor
actor MyGlobalActor: GlobalActor {
static let shared = MyGlobalActor()
}