It looks like the Swift new concurrency model doesn't work well with the old one. I've tried to incrementally adopt the new swift concurrency model for my new async functions ( using the async/await ceremony) but I quickly hit a wall when the traditional way of avoiding data race problem ( dispatching the tasks on a serial queue) does not work anymore. I know that the new model uses actors to deal with that but I thought the 2 world could live together but did't find a solution. To prove my problem please check the following code. The playground result show that some of the code of Task 2 is executed before Task 1 is completed and that makes the queue.async powerless and it goes agains developers expectation. Is there a way to have the Tasks serialised without using actors?
import Foundation
import _Concurrency
DispatchQueue.main.async{
print("1")
Task{
print("1.1")
try await Task.sleep(nanoseconds: 1000000000)
print("1.1.1")
}
print("1.1.1.1")
}
DispatchQueue.main.async{
print("2")
Task{
print("2.2")
try await Task.sleep(nanoseconds: 1000000000)
print("2.2.2")
}
print("2.2.2.2")
}
result:
1
1.1.1.1
2
2.2.2.2
1.1
2.2
2.2.2
1.1.1