本文介绍了从Health Kit获取一夜之间燃烧掉的卡路里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use this function to get calories burned from Health Kit after syncing with RunKeeper

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在一夜之间收集数据,我将无法每天获得单独的值.示例:

The problem is if I use RunKeeper to collect data overnight, I can't get separated values each day. Example:

4月17日:3.14月18日:4.2

Apr 17: 3.1Apr 18: 4.2

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

But inside my app, I only get the data below:Apr 17: 7.3

4月17日在健康"应用中:

Apr 17 in Health app:

有什么想法吗?

推荐答案

通过以下链接解决了该问题: https://developer.apple.com/reference/healthkit/hkstatisticscollectionquery

sovlved the problem with the link below:https://developer.apple.com/reference/healthkit/hkstatisticscollectionquery

这篇关于从Health Kit获取一夜之间燃烧掉的卡路里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 15:44