@DTS Engineer (Ziqiao),
Thank you for your quick response on my question. I've used an HKSampleQuery as you've suggested which retrieves the HRV. However when running the query, no results are returned for respiratory rate or body temperature. The following code shows a function with various calls (please comment/uncomment as needed):
func fetchData() {
let bodyTemperatureType = HKQuantityType.quantityType(forIdentifier: .respiratoryRate)!
let mostRecentPredicate = HKQuery.predicateForSamples(withStart: Date.distantPast, end: Date(), options: .strictEndDate)
let sampleQuery = HKSampleQuery(sampleType: bodyTemperatureType, predicate: mostRecentPredicate, limit: 1, sortDescriptors: [NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)]) { (query, samples, error) in
guard let samples = samples as? [HKQuantitySample], let sample = samples.first else {
print("No data available or error: \(String(describing: error))")
return
}
// Doesn't work!
let respiratoryRate = sample.quantity.doubleValue(for: HKUnit(from: "count/min"))
// Doesn't work!
//let bodyTemperature = sample.quantity.doubleValue(for: HKUnit.degreeCelsius())
// This works
//let hrv = sample.quantity.doubleValue(for: HKUnit.secondUnit(with: .milli))
//print("Body Temperature: \(hrv)°C")
//print("HRV: \(hrv)")
print("Resp rate: \(respiratoryRate)")
}
// Execute the query
healthStore.execute(sampleQuery)
}
I see no documentation from Apple why only HRV can be retrieved from an HKSampleQuery but not body temperature or respiratory rate.
Any ideas why this is?