简体   繁体   English

如何按小时计算HealthKit Swift 4的步数

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

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. 但如果用户的步骤从今天下午3:58开始到今天下午4:10结束,那么我将在这段时间内获得一个HKStatistics对象。 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. 我无法将这些数据分成两个样本,因为我需要在3-4 pm插槽和4-5 pm插槽中采取步骤。

  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)
        }
    }

Don't use HKSampleQuery for charting quantity types. 不要使用HKSampleQuery绘制数量类型图表。 HKStatisticsCollectionQuery is designed for this purpose and will split samples that fall into separate regions of your chart for you. HKStatisticsCollectionQuery旨在用于此目的,并将为您分割落入图表单独区域的样本。 See the documentation for examples of how to build the query and use its results. 有关如何构建查询并使用其结果的示例,请参阅文档

You're correct, you can't split the sample. 你是对的,你不能拆分样本。 That's the all the information that's available. 这就是所有可用的信息。 Steps are not stored step-by-step; 步骤不是一步一步存储的; they're aggregated into blocks to reduce power and storage requirements (mostly power; it's easier to accumulate a value in hardware and periodically read it than to query the real time clock every single time a step is detected). 它们被聚合成块以降低功率和存储要求(主要是功率;在硬件中累积值并且定期读取它比在每次检测到步骤时查询实时时钟更容易)。

In order to do what you're discussing, you'll need to average the steps over the period. 为了做你正在讨论的事情,你需要平均一段时间内的步数。 So if there were 100 steps over the period 3:58p to 4:07p, that averages 10 steps/minute, and you would allocate 20 steps to the 3p-4p block and 80 steps to the 4p-5p block. 因此,如果在3:58p至4:07p期间有100个步骤,则平均为10步/分钟,您将为3p-4p块分配20步,为4p-5p块分配80步。 That's the best information you have. 这是你拥有的最好的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM