add(_:to:completion:)
Associates the provided samples with the specified workout.
Declaration
func add(_ samples: [HKSample], to workout: HKWorkout, completion: @escaping @Sendable (Bool, (any Error)?) -> Void)func addSamples(_ samples: [HKSample], to workout: HKWorkout) async throwsParameters
- 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:
Mentioned in
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.