简体   繁体   中英

How to save an array of HKQuantitySamples (heart rate) to a workout

Apple's docs sample code saves an average heart rate HKQuantitySample to a workout via add method, however for a given workout I am trying to save all heart rate values taken during the workout, ie [HKQuantitySample] how can I do this? Below is my code adding the first value just to test, but i want to add them all?

 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
            }


            }
        }

You already have heartRateValues as [HKQuantitySample] so just do:

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
        }


        }
    }

Basically you create an array of samples add totalEnergyBurnedSample and totalDistanceSample and then whole heartRateValues array and then pass that sample argument in healthStore.add method...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM