查询healthkit以获得除锻炼样本外的所有activeEnergyBurned样本的最快方法

时间:2019-05-17 22:59:52

标签: ios swift health-kit

不知道如何创建单个查询来获取标题中所需的信息。

目前,我在伪代码中的代码是

    - for each workout, get the activeEnergyBurned samples via a query and add them to a set (allWorkoutSamplesSet)
    - get all samples, via a query, put them in a set (allSamples)
    - allSamples.subtract(allWorkoutSamplesSet)

这意味着它需要运行多个查询(number_of_workouts + 1)才能得出答案

循环锻炼的示例

        for workout in workouts {
            let workoutPredicate = HKQuery.predicateForObjects(from: workout)
            let compoundPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [datePredicate, workoutPredicate])
            let query = HKSampleQuery(sampleType: quantityType, predicate: compoundPredicate,
                                      limit: 0, sortDescriptors: nil) { (sampleQuery, results, error) -> Void in
                                        if let error = error {
                                            if error._code == self.dbInaccessibleErrCode {
                                                dbLocked = true
                                            } else {
                                                throwError = true
                                                errorMesg = error.localizedDescription
                                            }
                                        } else {
                                            if let workoutSamples = results as? [HKQuantitySample] {
                                                allWorkoutSamplesSet.formUnion(workoutSamples)
                                            }
                                        }
                                        blockFuncDispatchGroup.leave()
            }
            blockFuncDispatchGroup.enter()
            hkHealthStore.execute(query)
        }

我当前的代码有效,但是希望有一个查询解决方案。

0 个答案:

没有答案