0

we are trying to retrieve the daily step count and calories burn data from Health using HealthKit. The methods been used were:

stepCount = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)

caloriesBurn = HKSampleType.quantityType(forIdentifier: .activeEnergyBurned)

And, query executed using,

HKSampleQuery(sampleType: <stepCount or caloriesBurn>, predicate: last24hPredicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil)

But, depend upon the situations, we are facing data inconsistent issues while retrieving using HealthKit.

Happy situation: If we depend only on iPhone while doing the daily activities and data then retrieve using HealthKit into our app, both the step count and calories burn data are showing as same as in Health app.

Sad situation: If we use Apple Watch while doing daily activities, both the step count and calories burn won't get correctly into our app, always data mismatched.

As most of the clients are using gadgets to track their activities, data mismatched between Health app and ours, won't be acceptable.

Please suggest better solutions to overcome the issues.

Attached some screenshots of Health, Fitness and ours apps to show the data mismatched situations.

  1. Step count in Health app

enter image description here

  1. Step count and calories burn in Fitness app

enter image description here

  1. Step count and calories burn in ours app

enter image description here

We need to show exactly same value of all parameters form Health app to our own app, especially focus on step count, calories burn.

RUDRA S M
  • 19
  • 3
  • Aren't you using Statistical queries to get this data? Per apple's docs: Step count: "These samples use count units (described in HKUnit) and measure cumulative values (described in HKStatisticsQuery)." Active energy: "Active energy samples use energy units (described in HKUnit) and measure cumulative values (described in HKStatisticsQuery)." https://developer.apple.com/documentation/healthkit/queries/executing_statistical_queries – gggava Nov 02 '22 at 22:39
  • No, not really, so far used only HKSampleQuery. Let me try with HKStatisticsQuery. – RUDRA S M Nov 03 '22 at 05:34
  • @RUDRASM is your data mismatch issue fixed after using HKStatisticsQuery? – Sazzad Hissain Khan Apr 26 '23 at 04:48
  • 1
    @SazzadHissainKhan Yes I used HKStatisticsQuery and so far it matches with Health app data. – RUDRA S M Apr 27 '23 at 10:11

1 Answers1

0

I used HKStatisticsQuery for this purpose and solved the issue.

let now = Date()
let startOfDay = Calendar.current.startOfDay(for: now)
let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: now, options: .strictStartDate)
            
let query = HKStatisticsQuery(quantityType: energyType, quantitySamplePredicate: predicate, options: .cumulativeSum) { (_, statisticsOrNil, _) in
                
  guard let statistics = statisticsOrNil else {
     return
  }

  // perform further operation
}
RUDRA S M
  • 19
  • 3