I'd like to understand whether a function that may take time to execute will delay the execution of the function after it. Let me share the code:
First, we have a function that calculates an average value based on. This should be almost instant if the user has a few habits, but if the user has a high number of habits with a high number of entries, it might take a little longer.
var averagePercentage = 0.0
func calculateAveragePercentage() {
var percentages: [Double] { habits.map { $0.percentageFormed } }
if percentages.reduce(0,+) == 0 { averagePercentage = 0.0 }
else { averagePercentage = Double(percentages.reduce(0,+)) / Double(percentages.count) }
}
percentageFormed for each habit is calculated like this:
var percentageFormed: Double {
let validEntries = entries.filter({ $0.completed })
if validEntries.count == 0 { return 0.0 }
else { return Double(validEntries.count) / Double(daysToForm) }
}
What I am trying to understand, and I hope someone could help clarify this is the following: If in the viewDidLoad of a controller I call calculateAveragePercentage() and then I call a method that relies on this value, will the functions be executed in parallel? In which case there is a chance that setCircle() will be called before calculateAveragePercentage() has finished. There's no completion handler for the operations in calculateAveragePercentage() so I am not sure if there are situations where this can break or if setCircle() will wait for calculateAveragePercentage() to finish no matter how long it takes.
override func viewDidLoad() {
calculateAveragePercentage()
setCircle()
}