Things are even worse when I compile this project on Swift6. It bans this code completely!! With the error
Non-sendable type 'ModelLayer' passed in implicitly asynchronous call to main actor-isolated property 'gameModel' cannot cross actor boundary
But I call var model = await self.gameModel
that is, the current task stops and waits for the main actor! I don't understand what's wrong here
class ModelLayer {
@MainActor var gameModel = GameModel()
@MainActor func runGame() {
Task.detached {
await self.run()
}
}
func run() async {
while true {
try! await Task.sleep(nanoseconds: 1_000_000_000)
var model = await self.gameModel // ❌ Non-sendable type 'ModelLayer' passed in implicitly asynchronous call to main actor-isolated property 'gameModel' cannot cross actor boundary
model.step()
Task { @MainActor in
gameModel = model
}
}
}
}