ios - 从 Health Kit 获取夜间燃烧的卡路里

标签 ios swift healthkit

与 RunKeeper 同步后,我使用此功能从 Health Kit 获取消耗的卡路里

func getActiveEnergy(currentDate: Date ,completion: @escaping ((_ totalEnergy: Double) -> Void)) {
    let calendar = Calendar.current
    var totalBurnedEnergy = Double()
    let startOfDay = Int((currentDate.timeIntervalSince1970/86400)+1)*86400
    let startOfDayDate = Date(timeIntervalSince1970: Double(startOfDay))
    //   Get the start of the day
    let newDate = calendar.startOfDay(for: startOfDayDate)
    let startDate: Date = calendar.date(byAdding: Calendar.Component.day, value: -1, to: newDate)!

    //  Set the Predicates
    let predicate = HKQuery.predicateForSamples(withStart: startDate as Date, end: newDate as Date, options: .strictStartDate)

    //  Perform the Query
    let energySampleType = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.activeEnergyBurned)

    let query = HKSampleQuery(sampleType: energySampleType!, predicate: predicate, limit: 0, sortDescriptors: nil, resultsHandler: {
        (query, results, error) in
        if results == nil {
            print("There was an error running the query: \(error)")
        }

        DispatchQueue.main.async {

            for activity in results as! [HKQuantitySample]
            {
                let calories = activity.quantity.doubleValue(for: HKUnit.kilocalorie())
                totalBurnedEnergy = totalBurnedEnergy + calories
            }
            completion(totalBurnedEnergy)
        }
    })
    self.healthStore.execute(query)
}

问题是,如果我使用 RunKeeper 过夜收集数据,我无法每天获得单独的值。示例:

enter image description here

4 月 17 日:3.1 4 月 18 日:4.2

但在我的应用程序中,我只获取以下数据: 4 月 17 日:7.3

4 月 17 日在健康应用中:

enter image description here

有什么想法吗?

最佳答案

关于ios - 从 Health Kit 获取夜间燃烧的卡路里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43463966/

相关文章:

swift - 我如何在 SpriteKit 中在我的角色后面留下痕迹?

ios - swift 2.0 : Reading HealthKit HeartRate Data - unexpectedly found nil while unwrapping an Optional

ios - swift4:从服务器发送通知时 Firebase 通知不起作用

ios - 计算 iPhone 移动的短垂直距离

ios - 我们可以在 Xcode 6 beta 中将 swift 与 objective - c 一起使用吗?

ios - 使用iwatch快速提取心率代码

ios - 在现有的 HKWorkout 上添加元数据

objective-c - iOS:RestKit 加载对象并发送参数

ios - kCFCompareEqualTo 在 swift 中不可用吗?

swift - Kotlin 是否支持或有类似 Swift 中协议(protocol)组合的接口(interface)组合的 future 计划?