我正在尝试确定查询何时完成执行。我已经实现了一个完成回调,但是因为每个循环调用它,我必须设计一些方案来确定查询何时实际完成。还有另外一种方法吗?
我在执行查询时阻止主线程犹豫不决。
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)
|