HKStatisticsQuery返回的数据内容

时间:2019-05-26 06:36:39

标签: swift health-kit

我正在使用以下代码来消耗今天的能量:

    func getTodaysSummary(for type:HKQuantityType!, unit u:HKUnit!, completion: @escaping (Double) -> Void) {
        let stepsQuantityType = type

        let now = Date()
        let startOfDay = Calendar.current.startOfDay(for: now)
        let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: now, options: .strictStartDate)

        let query = HKStatisticsQuery(quantityType: stepsQuantityType!, quantitySamplePredicate: predicate, options: .cumulativeSum) { (_, result, error) in
            var resultCount = 0.0

            guard let result = result else {
                completion(resultCount)
                return
            }

            if let sum = result.sumQuantity() {
                resultCount = sum.doubleValue(for: u)
            }

            DispatchQueue.main.async {
                completion(resultCount)
            }
        }

        healthStore.execute(query)
    }

...

self.getTodaysSummary(for: HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned), unit: HKUnit.kilocalorie(), completion: { (energyBurned) in
  print("\(energyBurned)")
})

现在我需要了解,它返回什么数据?在阅读苹果文档等不同资源时,我阅读了这些数据,其中应包含所有活动,包括步行,游泳等。

但是在我的情况下,它仅返回数字680(我通过“健康”应用程序的“活动能量”屏幕上的+按钮手动添加了此值)

enter image description here

它绝对不包括我白天所做的步骤。 那么问题是,我需要分别计算那些卡路里吗? 还有一些iWatch环,似乎也未通过HKStatisticsQuery请求返回。我还应该分别计算它们以计算总燃烧能量吗?

1 个答案:

答案 0 :(得分:1)

iPhone不会自动计算有功电能。用户必须拥有Apple Watch才能将活动和基础能量样本自动写入HealthKit。