首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HealthKit - DietaryEnergyConsumed

HealthKit - DietaryEnergyConsumed
EN

Stack Overflow用户
提问于 2020-04-04 00:59:18
回答 1查看 183关注 0票数 0

晚上好,

我正在做一个获得.dietaryEnergyConsumed的项目,我能够得到一个单一的样本,但不知道如何获得当天的所有数据并对其进行汇总。任何帮助都是非常感谢的。

这里是获取数据的函数:

代码语言:javascript
复制
func getDietaryEnergy() {

    print("getDietaryEnergy()")

    guard let stepSampleType = HKQuantityType.quantityType(forIdentifier: .dietaryEnergyConsumed) else {

        print("Dietary Energy Sample Type is no longer available in HealthKit\n\n")

        return
    }

    self.getMostRecentSample(for: stepSampleType, completion: { (sample, error) in

        guard let sample = sample else {

            return
        }

        print(sample.quantity)

    })

}

这里是获取最新示例的查询:

代码语言:javascript
复制
func getMostRecentSample(for sampleType: HKSampleType,
                         completion: @escaping (HKQuantitySample?, Error?) -> Swift.Void) {

    print("getMostRecentSample()")

    //1. Use HKQuery to load the most recent samples.
    let mostRecentPredicate = HKQuery.predicateForSamples(withStart: Date.distantPast,
                                                          end: Date(),
                                                          options: .strictEndDate)

    let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate,
                                          ascending: false)

    let limit = 1

    let sampleQuery = HKSampleQuery(sampleType: sampleType, predicate: mostRecentPredicate, limit: limit, sortDescriptors: [sortDescriptor]) { (query, samples, error) in

        //2. Always dispatch to the main thread when complete.
        DispatchQueue.main.async {

            guard let samples = samples,
                let mostRecentSample = samples.first as? HKQuantitySample else {

                    completion(nil, error)
                    return
            }

            completion(mostRecentSample, nil)
        }
    }

    HKHealthStore().execute(sampleQuery)
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-10 17:23:10

因此,我创建了另一个函数,用于根据开始日期和结束日期执行和汇总示例。下面是代码:

代码语言:javascript
复制
//MARK: - Read Dietary Energy
func readDietaryEnergy(date: Date) {
    guard let energyType = HKSampleType.quantityType(forIdentifier: .dietaryEnergyConsumed) else {
        print("Sample type not available")
        return
    }

    let startDate = convertStartDate(StartDate: date)
    let endDate = convertEndDate(EndDate: date)
    let Predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: .strictStartDate)

    let dietaryEnergyQuery = HKSampleQuery(sampleType: energyType,
                                    predicate: Predicate,
                                    limit: HKObjectQueryNoLimit,
                                    sortDescriptors: nil) {
                                        (query, sample, error) in

                                        guard
                                            error == nil,
                                            let quantitySamples = sample as? [HKQuantitySample] else {
                                                print("Something went wrong: \(String(describing: error))")
                                                return
                                        }

                                        let total = quantitySamples.reduce(0.0) { $0 + $1.quantity.doubleValue(for: HKUnit.kilocalorie()) }
                                        DispatchQueue.main.async {
                                            self.userDietaryEnergy = total
                                        }
    }
    HKHealthStore().execute(dietaryEnergyQuery)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61022541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档