Skip to content

Instantly share code, notes, and snippets.

@KrishanMadushanka
Last active January 1, 2023 16:12
Show Gist options
  • Select an option

  • Save KrishanMadushanka/0cada5c66b46aa5afa774a25e852cf39 to your computer and use it in GitHub Desktop.

Select an option

Save KrishanMadushanka/0cada5c66b46aa5afa774a25e852cf39 to your computer and use it in GitHub Desktop.
func readTotalStepCount() {
guard let stepCountType = HKObjectType.quantityType(forIdentifier: .stepCount) else {
fatalError("*** Unable to get the step count type ***")
}
let query = HKStatisticsQuery.init(quantityType: stepCountType,
quantitySamplePredicate: get24hPredicate(),
options: [HKStatisticsOptions.cumulativeSum, HKStatisticsOptions.separateBySource]) { (query, results, error) in
let totalStepCount = results?.sumQuantity()!.doubleValue(for: HKUnit.count())
print("Total step count: \(totalStepCount ?? 0)")
if ((results?.sources) != nil){
for source in (results?.sources)! {
let separateSourceStepCount = results?.sumQuantity(for: source)!.doubleValue(for: HKUnit.count())
print("Seperate Source total step count: \(separateSourceStepCount ?? 0)")
}
}
}
healthStore.execute(query)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment