Last active
January 1, 2023 16:12
-
-
Save KrishanMadushanka/0cada5c66b46aa5afa774a25e852cf39 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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