I am fetching wrist temperature using HKStatisticsCollectionQuery on Apple Watch but the data doesn't match up with Health app itself.
iphone Health app (Cel.--Fer.) 36.51 -- 97.72, 36.76 -- 98.16, 36.26 -- 97.27, 36.08 -- 96.94
HKStatisticsCollectionQuery data 36.17 -- 97.10, 36.56 -- 97.82, 36.25 -- 97.26, 36.01 -- 96.83
Below is the code
func GetWristTemp() {
let healthStore = HKHealthStore()
var interval = DateComponents()
interval.day = 1
let currentDate = Date()
let startOfDay = Calendar.current.startOfDay(for: currentDate)
let anchorDate = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: currentDate)
let predicate = HKQuery.predicateForSamples(withStart: startOfDay,
end: currentDate,
options: [.strictStartDate])
let query = HKStatisticsCollectionQuery.init(quantityType: HKQuantityType(.appleSleepingWristTemperature),
quantitySamplePredicate: predicate,
options: [.discreteAverage, .separateBySource],
anchorDate: anchorDate ?? currentDate,
intervalComponents:interval)
query.initialResultsHandler = { query, results, error in
if let myResults = results {
for source in (myResults.sources()) {
myResults.enumerateStatistics(from: startOfDay, to: currentDate) { result, stop in
var quantity: HKQuantity?
quantity = result.averageQuantity(for: source)
if let quantity = quantity {
var data = 0.0
data = quantity.doubleValue(for: .degreeCelsius())
print("Temp - \(data)")
}
}
}
}
}
healthStore.execute(query)
}
Any help would be grateful. Thank you.
Fetched wrist temperature using Healthkit in Swift but the data doesn't match with Health app data.