@NeonTetra Thanks for the clarification! Also, I noticed that according to this Apple Documentation, if I have only async methods inside a Protocol I can mark @MainActor only MainActorClass
and not MainActorProtocol
.
Because async methods guarantee isolation by switching to the corresponding actor in the implementation.
But if I do that I am still getting the same error:
actor MyActor {
let property: MainActorProtocol
init(property: MainActorProtocol) {
self.property = property
}
func doSomething() async {
await property.foo() //Sending 'self.property' risks causing data races
}
}
protocol MainActorProtocol { // actor is not specified
func foo() async
}
@MainActor
class MainActorClass: MainActorProtocol {
func foo() async {
print("foo")
}
}