A builder object that constructs a workout incrementally.


class HKWorkoutBuilder : NSObject


Creating the Builder

init(healthStore: HKHealthStore, configuration: HKWorkoutConfiguration, device: HKDevice?)

Returns a new workout builder object that is not connected to a workout session or other data source.

var device: HKDevice?

The device associated with the workout.

var workoutConfiguration: HKWorkoutConfiguration

The configuration information for the workout.

Starting the Workout

func beginCollection(withStart: Date, completion: (Bool, Error?) -> Void)

Sets the workout’s start date and begins building the workout.

var startDate: Date?

The workout’s start date and time.

func elapsedTime(at: Date) -> TimeInterval

Calculates the duration of the workout at the specified time.

Associating Samples with the Workout

func add([HKSample], completion: (Bool, Error?) -> Void)

Adds a sample to be associated with the workout.

func seriesBuilder(for: HKSeriesType) -> HKSeriesBuilder?

Returns the series builder for the specified type, creating a new builder, if necessary.

func statistics(for: HKQuantityType) -> HKStatistics?

Returns the statistics calculated for matching samples added to the workout.

Adding Metadata to the Workout

var metadata: [String : Any]

The metadata the builder saves with the workout.

Adding Events to the Workout

var workoutEvents: [HKWorkoutEvent]

The list of events added to the workout.

Ending the Workout

func endCollection(withEnd: Date, completion: (Bool, Error?) -> Void)

Stops the collection of data, sets the workout’s end date, and deactivates the workout builder.

var endDate: Date?

The workout’s end date and time.

func finishWorkout(completion: (HKWorkout?, Error?) -> Void)

Creates the workout, using the samples and events added to the builder, and saves it to the HealthKit store.

func discardWorkout()

Stops the collection of data and discards the current results without saving the workout.


Inherits From

Conforms To

See Also

Workout Builder

class HKLiveWorkoutBuilder

A builder object that constructs a workout incrementally based on live data from an active workout session.

SpeedySloth: Creating a Workout

Use the Workout Builder API to start, stop, and to save workouts on Apple Watch.