Search code examples
iosswifthealthkit

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, i.e. [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
            }


            }
        }

Solution

  • 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...