Does the apple healthkit queries like HKStatisticsQuery
, HKSampleQuery
and HKStatisticsCollectionQuery
run asynchronously or do we need to explicitly run the queries in a separate thread ?
I just wrote the queries using any async way as follows and it works. I want to know whether I should put it inside an async block
private func readOxygrnSaturation(){
dispatchGroup.enter()
let quantityType = HKObjectType.quantityType(forIdentifier: .oxygenSaturation)!
let sampleQuery = HKSampleQuery.init(sampleType: quantityType,
predicate: nil,
limit: HKObjectQueryNoLimit,
sortDescriptors: nil,
resultsHandler: { (query, results, error) in
guard let samples = results as? [HKQuantitySample] else {
print(error!)
return
}
for sample in samples {
let mSample = sample.quantity.doubleValue(for: HKUnit(from: "%"))
self.oxygenSaturation.append(HealthData(unit: "%", startDate: self.dateFormatter.string(from: sample.startDate) , endDate: self.dateFormatter.string(from: sample.endDate), value: mSample))
}
self.dispatchGroup.leave()
})
self.healthStore .execute(sampleQuery)
}