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

Posted

技术标签:

【中文标题】从 Health Kit 获取一夜之间燃烧的卡路里【英文标题】:Get calories burned overnight from Health Kit 【发布时间】:2017-04-18 05:06:23 【问题描述】:

在与 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 日在健康应用中:

有什么想法吗?

【问题讨论】:

【参考方案1】:

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

【讨论】:

以上是关于从 Health Kit 获取一夜之间燃烧的卡路里的主要内容,如果未能解决你的问题,请参考以下文章

拜拜go圈圈,燃烧我的卡路里

燃烧我的卡路里——Flutter瘦内存瘦包之图片渲染组件

以编程方式获取/刷新 Health Kit 数据?

以编程方式获取/刷新 Health Kit 数据?

当我使用 Health App 中的 Delete All 操作时,Health kit 仍会返回已删除的数据

Health Kit 数据详细信息