繁体   English   中英

从 Health Kit 获得一夜之间燃烧的卡路里

[英]Get calories burned overnight from Health Kit

与 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 在一夜之间收集数据,我不能每天都得到分隔值。 例子:

在此处输入图片说明

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

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

4 月 17 日在健康应用程序中:

在此处输入图片说明

有任何想法吗?

暂无
暂无

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

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