I'm using following filters to fetch swimming activities from HealthKit.
For some users it fetches all workouts (pool && open water) but for other it skips some open water activities. See screenshot, all those swimming activities are not fetched by following code.
let startDate = Calendar.current.date(byAdding: .month, value: -1, to: Date())!
let endDate = Date()
let swimmingPredicate = HKQuery.predicateForWorkouts(with: .swimming)
let timePredicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: .strictStartDate)
let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [swimmingPredicate, timePredicate])
let query = HKSampleQuery(sampleType: .workoutType(),
predicate: predicate,
limit: HKObjectQueryNoLimit,
sortDescriptors: [.init(keyPath: \HKSample.startDate, ascending: false)],
resultsHandler: { [weak self] query, samples, error in
...
Could someone help with ideads what is missing in this case?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
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.
Calculation like that: pool length * number of laps = swimming distance BUT the Fitness app sometimes shows other values for distance per set (segment).
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?
Topic:
App & System Services
SubTopic:
Health & Fitness