watchOS 5: HKLiveWorkoutBuilder, HKWorkoutRouteBuilder and Workout Distance
Asked Answered
R

1

7

I am using HKLiveWorkoutBuilder & HKWorkoutRouteBuilder to track a workout on Apple Watch.

when calling:

[workoutBuilder finishWorkoutWithCompletion:^(HKWorkout * _Nullable workout, NSError * _Nullable error) {

}];

The returned workout object does not include the distance that the user travelled:

(lldb) po workout.totalDistance
nil

However the route is saved correctly.

Given that .totalDistance is read only, how should I set the distance of the workout in watchOS 5?

enter image description here

enter image description here

Recollect answered 10/9, 2018 at 21:24 Comment(0)
R
3

There are two parts to the answer here.

To get it working for walking or running you can use the following code:

workoutBuilder?.beginCollection(withStart: Date(), completion: { (success, error) in
    guard success == true else {
        #warning ("deal with failure")
        return
    }

    workoutBuilder?.dataSource = HKLiveWorkoutDataSource(healthStore:self.healthStore, workoutConfiguration: self.workoutConfig)

    // πŸ‘‡πŸ‘‡ this is the important bit πŸ‘‡πŸ‘‡
    workoutBuilder?.dataSource?.enableCollection(for: HKQuantityType.quantityType(forIdentifier: .distanceWalkingRunning)!, predicate: nil)

    workoutBuilder?.dataSource?.enableCollection(for: HKQuantityType.quantityType(forIdentifier: .heartRate)!, predicate: nil)
    workoutBuilder?.dataSource?.enableCollection(for: HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)!, predicate: nil)

})

You can adapt this for swimming and skiing.

There is currently no way to use workout builder to get distance on other types of outdoor activity, eg rowing.

--

Radar/Feedback details for if you want to Copy & Paste

Feedback No: FB5714600

Title: Support distance tracking for more exercise types

Body:

Currently live workout builder only includes support for tracking distance for the following types: - walking or running distance - skiing - swimming

I see two options. The best would be to allow something like:

workoutBuilder?.dataSource?.enableCollection(for: HKQuantityType.quantityType(forIdentifier: .distancePaddling)!, predicate: nil)

Another option would be to allow workouts to be mutable so that the distance could be added retrospectively.

I'm happy to provide further input if required. Our users love the Apple Watch, but it is frustrating for them that they can't store the distance of their paddle workouts in Health.

Recollect answered 28/9, 2018 at 20:57 Comment(8)
Is there anything else needed outside of your code snippet to save Active Calories and Total Calories? – Kyl
@AustinConlon not as far as I remember, they automatically collect some sensible defaults – Recollect
@Lewis42 did you end up filing a radar for this? – Apocarp
@Apocarp yes. "Engineering has determined that your bug report is a duplicate of another issue and will be closed" although once it got imported into feedback it hasn't been closed. I added the radar text to my answer so you can copy and paste if you want to dupe it. – Recollect
@Lewis42 thanks will do! If you were going to add distance to a mutable HKWorkout how would you collect distance samples? πŸ€” – Apocarp
@Apocarp you could collect them with a builder, I already do this, and then add the distance at the end. Not ideal, but better than no distance. – Recollect
@Lewis42 oh I thought the point of the radar was that sports like rowing (my example is hockey) do not return distance samples when using a builder? – Apocarp
@garysabo well it's not the builder that is the problem I don't think, it's the actual activity. if you try and create say a rowing activity just in the health app it won't let you add a distance. – Recollect

© 2022 - 2024 β€” McMap. All rights reserved.