Hi,
I've had trouble for a while now with HealthKit giving me different values if I make the request on iOS and WatchOS.
I am using the exact same method on both with the same parameters but I get vast differences in the results.
The code I am using to call HealthKit on both devices is:
let dateRange = HKQuery.predicateForSamples(withStart: Date().removeMonths(numberOfMonths: 1), end: Date().endOfDay())
let predicate: NSPredicate
predicate = NSCompoundPredicate(type: .and, subpredicates: [dateRange])
let query = HKStatisticsQuery(quantityType: HKQuantityType(.stepCount), quantitySamplePredicate: predicate, options: .cumulativeSum) { _, result, error in
if error != nil {
//print("Error fetching step count, or there is no data: \(error.localizedDescription), \(startDate) -> \(endDate)")
onComplete(0)
return
}
if let result, let sum = result.sumQuantity() {
let stepCount = sum.doubleValue(for: HKUnit.count())
DispatchQueue.main.async {
onComplete(Int(stepCount))
}
}
}
healthStore.execute(query)
}