Editing Apple Health Workouts

Sometimes my users would like to update an Apple Health workout after it's been saved. For example, maybe they forgot to end the workout and want to trim it. Or maybe it was a treadmill workout, and they want to add the stats from the treadmill.

The way I've attempted to do this in the past is to create a new HKWorkoutBuilder, call beginCollection, and add the data the user wants to keep from the original workout (events, activities, samples, workout route, and metadata with an incremented HKMetadataKeySyncIdentifier.

After all that, I call endCollection() and finishWorkout() on the builder.

Is this the right way to go about it?

For trimming a workout, it would be great to have a way to just duplicate a workout with an adjusted start and end time and have HealthKit do all the hard work of trimming data.

Answered by Engineer in 892786022

What you describe is actually the correct approach for replacing a workout sample. Note that if you trimmed the workout by updating the start or end dates, there is no need to do any trimming of the added samples. The statistics generated for your resulting workout would be correctly recomputed.

What you describe is actually the correct approach for replacing a workout sample. Note that if you trimmed the workout by updating the start or end dates, there is no need to do any trimming of the added samples. The statistics generated for your resulting workout would be correctly recomputed.

I believe in the past an error is generated if you attempt to save samples, activities, etc., with dates outside the range of the workout's start and end date. Are you saying that if I create a new workout and just copy everything over to it, only adjusting the start and end date of the workout, that this will work okay? What about, for example, an activity with an end date past the end date of the new workout?

Editing Apple Health Workouts
 
 
Q