Thank you, this is exactly what we needed.
Post
Replies
Boosts
Views
Activity
I don't think we are on the same page. That run was 10 miles, so it should have been long enough to retrieve information.
Better summary of what we are trying to do:
We are trying to get a split-by-split breakdown of each mile or kilometer. I attached the Apple UI of what we are looking to re-create.
The query above creates the json provided above. Appreicate your help. Thank you
Our results are attached.
apple_results.json
Hello, the HKQuantityTypeIdentifierDistanceWalkingRunning is grouped into one and not broken down into laps or segments or markers
Below, please find our query and results.
Here is our query
guard HKHealthStore.isHealthDataAvailable() else {
completion(nil, NSError(domain: "HealthKit", code: 1, userInfo: [NSLocalizedDescriptionKey: "HealthKit is not available on this device."]))
return
}
guard let distanceType = HKObjectType.quantityType(forIdentifier: .distanceWalkingRunning) else {
completion(nil, NSError(domain: "HealthKit", code: 2, userInfo: [NSLocalizedDescriptionKey: "Unable to create distanceWalkingRunning type."]))
return
}
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: .strictStartDate)
let query = HKSampleQuery(sampleType: distanceType, predicate: predicate, limit: HKObjectQueryNoLimit, sortDescriptors: nil) { _, samples, error in
if let error = error {
completion(nil, error)
return
}
let runningDistances: NSMutableArray = []
let distanceSamples = samples as? [HKQuantitySample]
if let distanceSamples = distanceSamples {
for sample in distanceSamples {
let distance = sample.quantity.doubleValue(for: HKUnit.meter())
let eventStartDate = self._dateFormatter.string(from: sample.startDate)
let eventEndDate = self._dateFormatter.string(from: sample.endDate)
let dict:[String:Any] = [
"startDate":eventStartDate,
"endDate":eventEndDate,
"distance": distance
]
runningDistances.add(dict)
}
}
completion(runningDistances, nil)
return
}
let healthStore = HKHealthStore()
healthStore.execute(query)
}