I've been experimenting with Swift Concurrency and noticed an interesting behavior.
If I write the following code inside an actor:
actor MyActor {
func runTasks() {
for i in 0..<100 {
let priority: TaskPriority = (i % 2 == 0) ? .high : .low
Task(priority: priority) {
print(i)
}
}
}
}
I expected the output to be out of order due to the use of different task priorities.
However, the numbers are printed in order (0 to 99), consistently.
I was under the impression that:
Task {}
creates a new concurrent task, andTask(priority:)
could affect the scheduling order.
So why does the output still follow a strict order?
Does creating Task
objects from within an actor method cause them to execute serially, even if those tasks are not explicitly bound to the actor?
Is this behavior reliable, or just an artifact of how the concurrency system behaves in this simple example?
Any clarification or pointers to official documentation would be appreciated to undersant thank a lot in advance