Title says it all, I've been scratching my head for a couple of days trying to work it out. Basing off the already stored data from previous workouts from the Watch:
Originally hoped to have used a HKStatisticsQuery
with a combination of HKStatisticsOptions.CumulativeSum
and the missing element being telling the query to get per kilometre data.
Now struggling with Workout events (pauses throwing my logic out)
So the output would be the distance 1km and the duration that km took. Any ideas for improvement, please help?
Thanks to Allan's direction below I've managed to get closer in two parts, one grabbing the workout:
print("👣HealthKit Workout:")
let healthStore:HKHealthStore = HKHealthStore()
let durationFormatter = NSDateComponentsFormatter()
var workouts = [HKWorkout]()
// Predicate to read only running workouts
let predicate = HKQuery.predicateForWorkoutsWithWorkoutActivityType(HKWorkoutActivityType.Running)
// Order the workouts by date
let sortDescriptor = NSSortDescriptor(key:HKSampleSortIdentifierStartDate, ascending: false)
// Create the query
let sampleQuery = HKSampleQuery(sampleType: HKWorkoutType.workoutType(), predicate: predicate, limit: 0, sortDescriptors: [sortDescriptor])
{ (sampleQuery, results, error ) -> Void in
if let queryError = error {
print( "There was an error while reading the samples: \(queryError.localizedDescription)")
}
workouts = results as! [HKWorkout]
let target:Int = 0
print(workouts[target].workoutEvents)
print("Energy ", workouts[target].totalEnergyBurned)
print(durationFormatter.stringFromTimeInterval(workouts[target].duration))
print((workouts[target].totalDistance!.doubleValueForUnit(HKUnit.meterUnit())))
self.coolMan(workouts[target])
self.coolManStat(workouts[target])
}
// Execute the query
healthStore.executeQuery(sampleQuery)
Second is processing the samples into splits:
let distanceType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierDistanceWalkingRunning)
let workoutPredicate = HKQuery.predicateForObjectsFromWorkout(workout)
let startDateSort = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: true)
let query = HKSampleQuery(sampleType: distanceType!, predicate: workoutPredicate,
limit: 0, sortDescriptors: [startDateSort]) {
(sampleQuery, results, error) -> Void in
// Process the detailed samples...
if let distanceSamples = results as? [HKQuantitySample] {
var count = 0.00
var firstStart = distanceSamples[0].startDate
let durationFormatter = NSDateComponentsFormatter()
for (index, element) in distanceSamples.enumerate() {
count += element.quantity.doubleValueForUnit(HKUnit.meterUnit())
//print("Item \(index): \(element.quantity) - \(count) - \(element.startDate) \(element.endDate)")
if count > 1000 {
print("We reached a kilometer")
/* Print The Split Time Taken */
print(durationFormatter.stringFromTimeInterval(round((distanceSamples[index+1].endDate.timeIntervalSinceDate(firstStart)))))
firstStart = distanceSamples[index+1].endDate;
count = 0.00
}
}
You'll need to process the raw samples from the workout in order and sum them together yourself to figure out where the splits are, I think. There isn't any clever way I can think of to compose an HKStatisticsQuery
or HKStatisticsCollectionQuery
that would somehow do that work for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With