Iñaki Gomez
Iñaki Gomez

Reputation: 1

Adding segments/splits into workout Swift

I am building a workout app that has an option to add segments to differentiate the stages of the training session. However, the segments are not visible in Fitness app.

Segments are added the following way:

func saveSegment(eventType: HKWorkoutEventType, startDate: Date, endDate: Date, inSeg: Int) async throws {
let dateInterval = DateInterval(start: startDate, end: endDate)
let event = HKWorkoutEvent(type: eventType, dateInterval: dateInterval, metadata: metadata)
try await builder?.addWorkoutEvents([event])
}

Inside Health -> Workouts -> Show All Data, the segments appear, but in Fitness app there are no segments. The workout is .paddleSports. I thought that maybe the workout type was the problem, but when I start the sessions from the native workout app, segments are added and shown in the Fitness app:

Segment example picture

In another post I saw suggested to add an event as .marker instead of .segment, but the result is the same, it does not appear. Is there something I am doing wrong? How can I show the segments into the Fitness app?

Thank you in advance.


Edit 1

The builder is defined as a HKLiveWorkoutBuilder

var builder: HKLiveWorkoutBuilder?

I also tried to add different metadata, even though I believe that should not affect the outcome.

Upvotes: 0

Views: 76

Answers (1)

Axel Rivera
Axel Rivera

Reputation: 1

I've been looking into this for years and this is what I've learned. You can't using a HKWorkoutBuilder. The segments will appear in the Health app, but not on the Fitness app. It doesn't matter if you use lap or marker the results are the same. I haven't tried this method, but I believe using a HKLiveWorkoutBuilder will work since this is what the Workout app from watchOS uses to build workouts.

Upvotes: 0

Related Questions