How to get steps count on hourly basis from HealthKit Swift 4

Rohan Mali

I need to plot graph for steps taken by user on hourly basis on any specific date. But if the user's steps start today at 3:58 pm and end today at 4:10 pm then I am getting just one HKStatistics object for this period of time. I am not able to break this data into two samples as I need to get steps taken in the 3-4 pm slot and the 4-5 pm slot.

  static func getSteps(date: Date, duration: DateComponents, completion: @escaping ([HKSample]) -> Void) {
        let quantityType : Set = [HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!]

        let stepsQuantityType = HKQuantityType.quantityType(forIdentifier: .stepCount)!
        let startOfDay = Calendar.current.startOfDay(for: date)
        if let endOfDay = Calendar.current.date(byAdding: duration, to: startOfDay) {
            var interval = DateComponents()
            interval.hour = 1
            let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: endOfDay, options: .strictStartDate)

            let query = HKSampleQuery.init(sampleType:stepsQuantityType,
                                                 predicate: predicate,
                                                 limit: HKObjectQueryNoLimit,
                                                 sortDescriptors: nil,
                                                 resultsHandler: { (query, results, error) in
                                                    guard let result = results else {

                                                                            return
                                                                        }
                                                   // print("result healthkit",result.description)
                                                    //print("Total count:",)
                                                    completion(result)
            })

            healthStore.execute(query)
        }
    }
Allan

Don't use HKSampleQuery for charting quantity types. HKStatisticsCollectionQuery is designed for this purpose and will split samples that fall into separate regions of your chart for you. See the documentation for examples of how to build the query and use its results.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to display steps count from HealthKit Swift 3

How to get data on an hourly basis

How to get data from oracle database in hourly basis

Get the count of above occurrences on an hourly basis using splunk query

How to get HealthKit total steps for previous dates

how to interpolate on hourly basis

How to get cycling duration from HealthKit for ios app using Swift

How can I get my app screen time on a hourly basis?

How to invalidate an entry from Spring cache based on hourly basis ?

How can I get daily average steps form HealthKit

Getting yesterdays steps from HealthKit

How can I get non-overlapping sleep samples from HealthKit in Swift?

HealthKit Swift getting today's steps

How to split a time into hourly basis in php

PowerBI- How to increment a column for an hourly basis?

Get devices from HealthKit

Swift 4: Are Strings reference counted & how to get that count

How to get text from clipboard in swift 4

How to get the most recent Weight entry from HealthKit data

How to get hourly RSI from different chart in TradingView Pine Script?

How to get diurnal cycle from hourly multiseries XTS in R?

Swift 4 - How to return a count of duplicate values from an array?

Get daily data from hourly

Get total step count for every date in HealthKit

How to rotate a log file in ubuntu on size basis hourly?

How to monitor Google Cloud Platform (GCP) costs on an hourly basis?

How can i schedule pyspark script on hourly basis on linux enviroment

Swift SQLite how to get row.count from table?

Count hourly data from log file