I found code example of parallel execution from this site:
In contrast, if you change both Task initializers to Task.detached, you’ll see “In Task 1” and “In Task 2” get intermingled as both execute at the same time.
and adapt it to async let calls, but for some reason this code is not executed in parallel...
struct ContentView: View {
var body: some View {
Task {
async let result = runForLoop1()
async let result2 = runForLoop2()
}
return Text("Hello, world!")
}
}
func runForLoop1() async -> Int {
for i in 1...1000 {
print("In Task 1: \(i)")
}
return 100_000
}
func runForLoop2() async -> Int {
for i in 1...1000 {
print("In Task 2: \(i)")
}
return 100_000
}
I use Xcode Version 13.2.1 (13C100)
Edit
I also try to run @Rob example on Mac Console app, but this app crush:
Task {
async let result = runForLoop1()
async let result2 = runForLoop2()
}
func runForLoop1() async -> Int {
var i = 0
let start = Date()
while Date().timeIntervalSince(start) < 1 {
print("In Task 1: \(i)")
i += 1
}
return 100_000
}
func runForLoop2() async -> Int {
var i = 0
let start = Date()
while Date().timeIntervalSince(start) < 1 {
print("In Task 2: \(i)")
i += 1
}
return 100_000
}
Edit 2
I also try to run @Rob example on macOS standard app, but this app crush:
Code:
struct ContentView: View {
var body: some View {
Task {
async let result = runForLoop1()
async let result2 = runForLoop2()
await print(result, result2)
}
return Text("Hello, world!")
.padding()
}
}
func runForLoop1() async -> Int {
var i = 0
let start = Date()
while Date().timeIntervalSince(start) < 1 {
print("In Task 1: \(i)")
i += 1
}
return 100_000
}
func runForLoop2() async -> Int {
var i = 0
let start = Date()
while Date().timeIntervalSince(start) < 1 {
print("In Task 2: \(i)")
i += 1
}
return 100_000
}