I am trying to determine when a query has finished executing. I have implemented a completion callback however, as it is called with each loop I would have to devise some scheme to determine when the query has actually completed. Is there another way?
I am hesitant to block the main thread while the query is executing.
func updateData(completion: @escaping (Bool) -> () ) {
.....
statsCollection.enumerateStatistics(from: startDate, to: endDate as Date) { [unowned self] statistics, stop in
if let quantity = statistics.sumQuantity() {
DispatchQueue.main.async(execute: {
let date = statistics.startDate
let value = quantity.doubleValue(for: HKUnit.count())
self.setCount(value, forDate: date)
completion(true)
})
}
}
}
healthStore.execute(query)
|