函数在第二次 healthkit 查询完成之前返回

Func returning before second healthkit query complete

我正在尝试从 HealthKit 获取给定日期的 stepsQuantityType 和 kjActiveQuantityType。

我可以让两个查询单独工作,如果有趣的话,但是当试图在同一个函数中 运行 它们一起工作时,在第二个查询完成之前执行 returns 给出索引超出范围错误因为数组的索引 1 尚未填充。

如有任何帮助,我们将不胜感激。

我使用

调用函数
getTodaysStepsAndKJ { arrayData in
            self.stepsHolder = arrayData[0]
            self.kjHolder = arrayData[1] * 4.184
            DispatchQueue.main.async {self.stepsLabel.text = arrayData[0].description;
                                      self.kjLabel.text = (arrayData[1] * 4.184).description}
            self.saveDataToCloud()
        }

函数是

func getTodaysStepsAndKJ(completion: @escaping (Array<Double>) -> Void) {
      let stepsQuantityType = HKQuantityType.quantityType(forIdentifier: .stepCount)!
      let kjActiveQuantityType = HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)!
      var resultArray = Array<Double>();
      let now = Date()
      let startOfDay = Calendar.current.startOfDay(for: now)
      let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: now, options: .strictStartDate)

      let query = HKStatisticsQuery(quantityType: stepsQuantityType, quantitySamplePredicate: predicate, options: .cumulativeSum) { _, result, _ in
          guard let result = result, let sum = result.sumQuantity() else {
            resultArray.insert(0.0, at: 0)
            completion(resultArray)
              return
          }
        resultArray.insert(sum.doubleValue(for: HKUnit.count()), at: 0)
          completion(resultArray)

      }
        let query2 = HKStatisticsQuery(quantityType: kjActiveQuantityType, quantitySamplePredicate: predicate, options: .cumulativeSum) { _, result, _ in
            guard let result = result, let sum = result.sumQuantity() else {
                resultArray.insert(0.0, at: 1)
                completion(resultArray)
                return
            }
            resultArray.insert(sum.doubleValue(for: HKUnit.kilocalorie()), at: 1)
            completion(resultArray)
        }
    healthStore.execute(query)
    healthStore.execute(query2)
  }

当拆分和 运行 作为两个函数时,它们都可以正常工作。

如果你在查询 1 中使用 query2 ...那么它就可以工作......大致类似于以下...未经测试的代码

func getTodaysStepsAndKJ(completion: @escaping (Array<Double>) -> Void) {
      let stepsQuantityType = HKQuantityType.quantityType(forIdentifier: .stepCount)!
      let kjActiveQuantityType = HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)!
      var resultArray = Array<Double>();
      let now = Date()
      let startOfDay = Calendar.current.startOfDay(for: now)
      let predicate = HKQuery.predicateForSamples(withStart: startOfDay, end: now, options: .strictStartDate)

      let query = HKStatisticsQuery(quantityType: stepsQuantityType, quantitySamplePredicate: predicate, options: .cumulativeSum) { _, result, _ in
          guard let result = result, let sum = result.sumQuantity() else {
            resultArray.insert(0.0, at: 0)
            completion(resultArray)
              return
          }
        resultArray.insert(sum.doubleValue(for: HKUnit.count()), at: 0)
           let query2 = HKStatisticsQuery(quantityType: kjActiveQuantityType, quantitySamplePredicate: predicate, options: .cumulativeSum) { _, result, _ in
            guard let result = result, let sum = result.sumQuantity() else {
                resultArray.insert(0.0, at: 1)
                completion(resultArray)
                return
            }
            resultArray.insert(sum.doubleValue(for: HKUnit.kilocalorie()), at: 1)
            completion(resultArray)
        }
      healthStore.execute(query2)
      }

    healthStore.execute(query)

  }