我真的想要执行HKSampleQuery
. 但是,执行查询后我总是无法立即获得结果。
我的情况如下(错误处理代码被删除):
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
// get the latest step count sample
let stepSampleQuery: HKSampleQuery = HKSampleQuery(sampleType: (stepCountQty)!,
predicate: nil,
limit: 1,
sortDescriptors: [sortDescriptor]) {
(query, results, error) -> Void in
if let result = results as? [HKQuantitySample] {
// result[0] is the sample what I want
self.lastStepDate = result[0].startDate
print("readLastStep: ", self.lastStepDate)
}
}
self.healthStore.executeQuery(query)
// now, I want to use the "self.lastStepDate"
// But, I cannot get the appropriate value of the variable.
我认为代码不会逐步运行。resultHandler
of什么时候HKSampleQuery
跑?我真的希望处理程序代码在我使用查询结果之前运行。