Could anyone please clarify how the concurrency checking works in my case? I have an actor that calls the foo method of a @MainActor class:
actor MyActor {
let property: MainActorClass
init(property: MainActorClass) {
self.property = property
}
func doSomething() async {
await property.foo()
}
}
@MainActor
class MainActorClass {
func foo() async {
print("foo")
}
}
It compiles with Swift 6, but when I’m trying to abstract my MainActorClass with MainActorProtocol:
actor MyActor {
let property: MainActorProtocol
init(property: MainActorProtocol) {
self.property = property
}
func doSomething() async {
await property.foo()
}
}
@MainActor
protocol MainActorProtocol {
func foo() async
}
class MainActorClass: MainActorProtocol {
func foo() async {
print("foo")
}
}
I am getting an error:
Sending ‘self.property’ risks causing data races
What’s the difference between these two approaches and how can I abstract my MainActorClass with a Protocol?
I tried to add Sendable to a Protocol but it looks a little bit cumbersome to add Sendable to almost every protocol in a project.
MasterWatcher is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.