HealthKit步骤延迟

时间:2018-12-01 19:11:24

标签: ios swift health-kit

我正在使用HealthKit在我的应用程序中显示步骤,但这非常延迟。我正在使用其他可以实时显示步骤的应用程序,但是感觉HealtKit延迟了几分钟吗?

这是接收步骤的代码:

func queryStepsSum() {
        let sumOption = HKStatisticsOptions.cumulativeSum
        let startDate = NSDate().dateByRemovingTime()
        let endDate = NSDate()
        let predicate = HKQuery.predicateForSamples(withStart: startDate as Date, end: endDate as Date, options: [])
        let statisticsSumQuery = HKStatisticsQuery(quantityType: healthKitManager.stepsCount!, quantitySamplePredicate: predicate, options: sumOption) { [unowned self] (query, result, error) in
            if (result != nil) {
                if let sumQuantity = result?.sumQuantity() {
                    DispatchQueue.main.async {
                        let numberOfSteps = Int(sumQuantity.doubleValue(for: self.healthKitManager.stepsUnit))
                        self.stepsLabel.text = "\(numberOfSteps)"
                    }
                }
            }
        }
        healthKitManager.healthStore?.execute(statisticsSumQuery)
    }

有什么提示吗?

1 个答案:

答案 0 :(得分:0)

尝试使用设置了HKStatisticsCollectionQuery属性的updateHandler。执行长时间运行的查询将向HealthKit指示您的应用程序打算向用户显示实时结果。