Instance Method

add(_:to:completion:)

Associates the provided samples with the specified workout.

Declaration

func add(_ samples: [HKSample], to workout: HKWorkout, completion: @escaping (Bool, Error?) -> Void)

Parameters

samples

An array containing HKCategorySample or HKQuantitySample objects.

workout

The workout object you are adding samples to.

completion

A block that this method calls as soon as the add-samples operation is complete. This block is passed the following parameters:

success

A Boolean value. This parameter contains true if the samples were successfully added to workout; otherwise, false.

error

An error object. If an error occurred, this object contains information about the error; otherwise, it is set to nil.

Discussion

This method operates asynchronously. As soon as the add-samples operation is finished, this method calls the completion block on a background queue. You must save the workout to the HealthKit store before you can add any samples to it. You can save the samples before calling this method, but doing so is not required. This method automatically saves any unsaved samples when it successfully adds them to the workout.

To query for all the samples associated with a workout, add the workout to the query’s predicate. For example, the query’s predicateForObjects(from:) method creates a predicate object that matches only samples associated with the provided workout.

For more information on workouts and associated samples, see HKWorkout.

See Also

Managing Workouts

func splitTotalEnergy(HKQuantity, start: Date, end: Date, resultsHandler: (HKQuantity?, HKQuantity?, Error?) -> Void)

Calculates the active and resting energy burned based on the total energy burned over the given duration.

Deprecated