I have a list of objects and I'm currently looping over the list and executing a method for each of those objects like this:
var houses = [House]()
for house in houses {
house.price = calculateHousePrice(house: house)
}
// continue here once all the prices have been calculated
Calculating the house price takes a long time, and is not dependent on the result of other calculations.
How can I use parallelism in Swift to speed this loop up? I've had a look at GCD, but I'm new to Swift and not sure what I should be using.
I've tried this:
for house in houses {
DispatchQueue.global(qos: .background).async { [self] in
house.price = calculateHousePrice(house: house)
}
}
// continue here once all the prices have been calculated
but the loop completes before all the task have completed.
Changing async to sync helps, but I feel that I'm no better off than with the initial code.
async/awaitwith/without withTaskGroup