Apple的docs示例代码通过HKQuantitySample方法将平均心率add保存到训练中,但是对于给定的训练,我正在尝试保存在训练过程中获取的所有心率值,即[HKQuantitySample]如何做到这一点?下面是我的代码添加第一个值只是为了测试,但我想添加它们全部?

 var heartRateValues = [HKQuantitySample]()

 func processHeartRateSamples(_ samples: [HKQuantitySample]) {
        for sample in samples {
            heartRateValues.append(sample)
        }
    }

private func addSamples(toWorkout workout: HKWorkout, from startDate: Date, to endDate: Date) {
        // Create energy and distance samples
        let totalEnergyBurnedSample = HKQuantitySample(type: HKQuantityType.activeEnergyBurned(),
                                                       quantity: totalEnergyBurnedQuantity(),
                                                       start: startDate,
                                                       end: endDate)

        let totalDistanceSample = HKQuantitySample(type: HKQuantityType.distanceWalkingRunning(),
                                                   quantity: totalDistanceQuantity(),
                                                   start: startDate,
                                                   end: endDate)





        // Add samples to workout
        healthStore.add([totalEnergyBurnedSample, totalDistanceSample, heartRateValues.first!], to: workout) { (success: Bool, error: Error?) in
            guard success else {
                print("Adding workout subsamples failed with error: \(String(describing: error))")
                return
            }


            }
        }

最佳答案

你已经有了heartRateValues[HKQuantitySample]一样,那么就这样做:

private func addSamples(toWorkout workout: HKWorkout, from startDate: Date, to endDate: Date) {
    // Create energy and distance samples
    let totalEnergyBurnedSample = HKQuantitySample(type: HKQuantityType.activeEnergyBurned(),
                                                   quantity: totalEnergyBurnedQuantity(),
                                                   start: startDate,
                                                   end: endDate)

    let totalDistanceSample = HKQuantitySample(type: HKQuantityType.distanceWalkingRunning(),
                                               quantity: totalDistanceQuantity(),
                                               start: startDate,
                                               end: endDate)

    let samples = [HKQuantitySample]()
    samples.append(totalEnergyBurnedSample)
    samples.append(totalDistanceSample)
    samples.append(contentsOf: heartRateValues)

    // Add samples to workout
    healthStore.add(samples, to: workout) { (success: Bool, error: Error?) in
        guard success else {
            print("Adding workout subsamples failed with error: \(String(describing: error))")
            return
        }


        }
    }

基本上你创建了一个样本数组addtotalEnergyBurnedSampletotalDistanceSample然后整个heartRateValues数组,然后在sample方法中传递healthStore.add参数。。。

关于ios - 如何将HKQuantitySample(心率)数组保存到锻炼中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46835438/

10-13 04:07