使用Swift从HealthKit获取最近30天的bodyMass数据

时间:2019-06-25 19:16:00

标签: ios swift health-kit

我现在正在学习Swift和HealthKit,并且在获取数据方面有些卡住。 我已经成功查询了过去30天内已打印到控制台的dietaryCaloriesConsumed

当我尝试对重量数据执行相同的操作时,没有输出。 尽管我可以通过倒数日期并仅使用第一个样本来获取特定日期的体重数据。

我现在的代码:

    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进行进一步处理的最佳方法是什么?

非常感谢:)

1 个答案:

答案 0 :(得分:0)

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

    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以及日期