title: HealthKit – How to get Running Activity in splits of distance (each kilometre)?
body: <p>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 <strong>workouts</strong> from the  Watch:</p>
  
  <p>Originally hoped to have used a <code>HKStatisticsQuery</code> with a combination of <code>HKStatisticsOptions.CumulativeSum</code> and the missing element being telling the query to get per kilometre data. </p>
  
  <p>Now struggling with Workout events (pauses throwing my logic out)</p>
  
  <p>So the output would be the distance 1km and the duration that km took. 
  Any ideas for improvement, please help?</p>
  
  <p>Thanks to Allan's direction below I've managed to get closer in two parts, one grabbing the workout:</p>
  
  <pre><code> 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 ) -&gt; 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)
  </code></pre>
  
  <p>Second is processing the samples into splits:</p>
  
  <pre><code>  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) -&gt; 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 &gt; 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
                      }
                  }
  </code></pre>