首页 > 解决方案 > 使用 Swift 从 HealthKit 获取过去 30 天的 bodyMass 数据

问题描述

我现在正在学习 Swift 和 HealthKit 并且有点卡在检索数据上。我已经成功查询dietaryCaloriesConsumed了最近 30 天的我的打印到控制台的信息。

当我尝试对体重数据做同样的事情时,我没有得到任何输出。尽管我可以通过倒数日期并仅使用第一个样本来获取特定日期的体重数据。

我现在的代码:

    func getWeightData2(forDay days: Int, completion: @escaping ((_ weight: Double?, _ date: Date?) -> Void)) {
    // Getting quantityType as stepCount
    guard let weight = HKObjectType.quantityType(forIdentifier: .bodyMass) else {
        print("*** Unable to create a step count type ***")
        return
    }

    let now = Date()
    let startDate = Calendar.current.date(byAdding: DateComponents(day: -days), to: now)!

    var interval = DateComponents()
    interval.day = 1

    var anchorComponents = Calendar.current.dateComponents([.day, .month, .year], from: now)
    anchorComponents.hour = 0
    let anchorDate = Calendar.current.date(from: anchorComponents)!

    let query = HKStatisticsCollectionQuery(quantityType: weight,
                                            quantitySamplePredicate: nil,
                                            options: [.mostRecent],
                                            anchorDate: anchorDate,
                                            intervalComponents: interval)
    query.initialResultsHandler = { _, results, error in
        guard let results = results else {
            print("ERROR")
            return
        }

        results.enumerateStatistics(from: startDate, to: now) { statistics, _ in
            if let sum = statistics.sumQuantity() {
                let weight = Int(sum.doubleValue(for: HKUnit.gramUnit(with: .kilo)).roundToDecimal(2))
                                  print("Weight Tracked: \(weight), on date: \(statistics.endDate)")
                //                    completion(calories, statistics.endDate)
                return
            }
        }
        completion(nil, nil)
    }
    healthStore.execute(query)
}

有了这个,我没有得到任何输出。稍微改变一下,dietaryCaloriesConsumed我就得到了 30 天的卡路里,每天总结(带选项cumulativeSum

真正起作用的是检索特定日期的数据,如下所示:

    func getWeightData(forDay days: Int) {
    let quantityType: HKQuantityType = HKQuantityType.quantityType(forIdentifier: .bodyMass)!

    // fetch last 30 days
    let startDate = Date.init(timeIntervalSinceNow: TimeInterval(-days*24*60*60))
    let endDate = Date()
    let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate)

    let sampleQuery = HKSampleQuery.init(sampleType: quantityType, predicate: predicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil) { (query, samples, error) in
        DispatchQueue.main.async {
            guard let samples = samples else { return }
            let mostRecentSample = samples.first as? HKQuantitySample
            let bodyMass = mostRecentSample?.quantity.doubleValue(for: HKUnit.gramUnit(with: .kilo)).roundToDecimal(2)
            print("Weight tracked: \(bodyMass) on date: \(endDate)")
    }
    }
    healthStore.execute(sampleQuery)
}

从我的 ViewController 我调用这些函数是这样的:

profileStore.getTotalCalories(forDay: 30) {_,_   in
    }

profileStore.getWeightData(forDay: 30)

这行得通,但我相信还有很大的批准空间。

任何想法我的第一个代码做错了什么?获得 n 天价值dietaryCaloriesConsumedbodyMass进行进一步处理的最佳方法是什么?

非常感谢 :)

标签: iosswifthealthkit

解决方案


我刚刚解决了我自己的问题:

    func getWeightData(forDay days: Int, completion: @escaping ((_ weight: Double?, _ date: Date?) -> Void)) {
    // Getting quantityType as stepCount
    guard let bodyMassType = HKObjectType.quantityType(forIdentifier: .bodyMass) else {
        print("*** Unable to create a bodyMass type ***")
        return
    }

    let now = Date()
    let startDate = Calendar.current.date(byAdding: DateComponents(day: -days), to: now)!

    var interval = DateComponents()
    interval.day = 1

    var anchorComponents = Calendar.current.dateComponents([.day, .month, .year], from: now)
    anchorComponents.hour = 0
    let anchorDate = Calendar.current.date(from: anchorComponents)!

    // Note to myself:: StatisticsQuery!! Nicht Collection! Option .mostRecent. Achtung, unten auch setzen!!
    let query = HKStatisticsCollectionQuery(quantityType: bodyMassType,
                                            quantitySamplePredicate: nil,
                                            options: [.mostRecent],
                                            anchorDate: anchorDate,
                                            intervalComponents: interval)
    query.initialResultsHandler = { _, results, error in
        guard let results = results else {
            print("ERROR")
            return
        }

        results.enumerateStatistics(from: startDate, to: now) { statistics, _ in
            // hier wieder .mostRecent!
            if let sum = statistics.mostRecentQuantity() {
                let bodyMassValue = sum.doubleValue(for: HKUnit.gramUnit(with: .kilo)).roundToDecimal(2)
                completion(bodyMassValue, statistics.startDate)
                return
            } 
        }
    }
    healthStore.execute(query)
}

这会加载最后n几天的数据并返回 bodyMass 以及日期


推荐阅读