Myk
Myk

Reputation: 1019

How to add total distance to HKLiveWorkouts

I am using the new HKLiveWorkoutBuilder to create a Healthkit Workout. All is well except that I can't figure out how to give the workout a total distance. I add the distance sample which works (although it shows the unit twice in Health e.g. 200 mi mi) but when querying the workout later it does not have a total distance, and even in the workout builders finishWorkout() function the workout does not have a total distance. Does anyone know how I can get total distance on Live Workouts?

Upvotes: 1

Views: 839

Answers (2)

Myk
Myk

Reputation: 1019

Seems like setting the activity type on the HKWorkoutConfiguration() is the answer. I was using "other" as the activity type and for whatever reason the resulting HKWorkout does not have a total distance. I switched the activity type to walking and it works fine.

Upvotes: 0

TealShift
TealShift

Reputation: 962

It doesn't look like HKLiveWorkoutBuilder creates an HKWorkout for you, but instead only provides you with incremental data as the workout session progresses.

Since HKWorkout is immutable, you should create one only once all its data is collected. Then once you want to save the workout, you could re-query all relevant data from the Health store, or use the samples collected realtime.

Use the HKQuantityTypeIdentifier for the "distance traveled" e.g. : https://developer.apple.com/documentation/healthkit/hkquantitytypeidentifier/1615230-distancewalkingrunning to query for the value before initializing the HKWorkout.

Upvotes: 1

Related Questions