How to get the same swimming distance like in the Fitness app

Hello Apple Community,

What does approach use the Fitness app for swimming distance calculation per set (segment)?

I've tried 2 options but all of them have different values than in the Fitness app.

  1. Calculation like that: pool length * number of laps = swimming distance BUT the Fitness app sometimes shows other values for distance per set (segment).

  2. Fetch all distance values via HKQuantityTypeIdentifier.distanceSwimming (HKSampleQuery`) and than try to match distance values with set (segment) duration. Again I got other values for swimming distance per set, values are bigger than in the Fitness app.

let healthStore = HKHealthStore()
let distanceType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.distanceSwimming)!

let predicate = HKQuery.predicateForSamples(withStart: startDate as Date, end: endDate as Date?, options: .strictStartDate)

let query = HKSampleQuery(sampleType: distanceType, predicate: predicate, limit: HKObjectQueryNoLimit, sortDescriptors: [.init(keyPath: \HKSample.startDate, ascending: true)], resultsHandler: { (query, results, error) in
    
    if let error {
        Logger.e("\(error)")
        continuation.resume(returning: nil)
    }
    
    continuation.resume(returning: results)
})

healthStore.execute(query)

Is it possible to get the same swimming distance like in the Fitness app per set via HealthKit?

Would you mind to share more information, maybe screenshots, to show the values Fitness.app presents and how they are different from your calculation? I'd take a look and see if there is something to comment.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Hello,

We have 2 issues on our side.

1. We struggle with distances in Fitness app.

As we can see with this activity (a real activity), when we compare Total Distance with the Sum of All Sets, both distances are different.

Total Distance = 1050 m

Sum of All Sets = 1025 m

There are 25 m difference between total values and set details.

2. Then, on our side, we have 2 options to calculate swimming distance but we don’t know which one is the best to use.

a Option 1 : pool length * number of laps = swimming distance

With this option, we see that distance is matching total distance from Fitness app. But for some activities (are shown above), some distance per set (segment) are different than from Fitness app.

b Option 2 : Fetch all distances values via HKQuantityTypeIdentifier.distanceSwimming (HKSampleQuery) .

With this option, we see that distance is matching sum of all sets distance from Fitness app. But for some activities (are shown above), total distance is different than from Fitness app.

Also we added more screenshots from the Fitness app and raw data from that workout sesseion.

Thanks for providing the screenshots, which are quite helpful.

Your screenshots show the data per set. From there, I calculated the total of each activity and got the following:

  • Kick-board: 50
  • Breaststroke: 75
  • Freestyle: 925

This is different from the data shown in Fitness.app, which is:

  • Kick-board: 50
  • Breaststroke: 100
  • Freestyle: 875

This inconsistency is probably because Fitness.app does something wrong, and so I'd suggest that you file a feedback report with all the information, and share your report ID here. I can probably use your report to see what the HealthKit folks have to say.

The total of all activities from the per-set data, which is 1050, is the same as the Distance in Fitness.app.

To get the per-set data and the total distance for a swimming workout, you might consider the following:

  1. Use HKWorkout.statistics(for:) + .distanceSwimming to retrive the total distance.

  2. Use HKWorkout.workoutEvents to retrive the per-segment (.segment) or per-lap (.lap) data.

You have the real swimming workout data, so if you don't mind, please give them a try and share if what you get from the APIs matches the real situation.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Hello,

Thanks for your response!

We wrote the feedback two weeks ago but no answer. What can we do to solve this issue?

How to get the same swimming distance like in the Fitness app
 
 
Q