HealthKit步骤在第一次查询时显示错误的数据并在第二次提供正确的数据?

时间:2017-04-16 06:36:02

标签: ios swift3 health-kit hkhealthstore

private func readDataFromHealthKit(_ forIdentifier: HKQuantityTypeIdentifier,_ completionHandler : @escaping (Double?,NSError?) -> Void) {

    let endDate = currentDate 
    let startDate = calendar.startOfDay(for: currentDate)

    let type = HKSampleType.quantityType(forIdentifier: forIdentifier)
    let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: [])
    // The actual HealthKit Query which will fetch all of the steps and sub them up for us.
    let statisticsQuery = HKStatisticsQuery(quantityType: type!, quantitySamplePredicate: predicate, options: .cumulativeSum) { (query, results, error) in
        var resultCount = 0.0
        if let result = results?.sumQuantity() {
            if forIdentifier == .distanceWalkingRunning {
                resultCount = result.doubleValue(for: HKUnit.meter())
            } else {
                resultCount = result.doubleValue(for: HKUnit.count())
            }
        }
        completionHandler(resultCount,nil)
    }
    healthKitStore.execute(statisticsQuery)
}

我正在从healthkit获取步骤和距离。当我第一次调用此函数时,我得到了错误的值。但如果再次调用此函数,我会得到正确的结果。

0 个答案:

没有答案