Hello,
How can I read the dive count and total underwater time for a Dive workout from Apple Health?
Is this information available through HealthKit, and if so, which APIs or workout metadata keys should I use?
Thanks!
Stéphane
Hello,
How can I read the dive count and total underwater time for a Dive workout from Apple Health?
Is this information available through HealthKit, and if so, which APIs or workout metadata keys should I use?
Thanks!
Stéphane
One approach is to use the following function as a starting point. It returns a TimeInterval array. First, it creates a predicate for only dive workouts and queries them. Using .map it builds a TimeInterval (durations) array for those workouts. The count of this array is the number of dives. The total dive time can be calculated with .reduce to sum the array.
func divingWorkoutDurations(store: HKHealthStore) async throws -> [TimeInterval] {
let predicate = HKQuery.predicateForWorkouts(with: .underwaterDiving)
let descriptor = HKSampleQueryDescriptor(
predicates: [.workout(predicate)],
sortDescriptors: []
)
let diveWorkoutDurations = try await descriptor.result(for: store).map { $0.duration }
return diveWorkoutDurations
}
let totalDiveTime = durations.reduce(0, +)
Hello,
Thanks.
However doing this does not match at all what the Apple Form app is showing. Neither the duration, nor the number of dives.
Maybe the Apple Form app is broken.
See the attached CSV file, there are 20 water depth samples, it does not match 5 dives.... And the sum of the durations is 33 seconds, not 46 seconds
Stéphane