title: Healthkit per hour Activity summary containing steps, distance and calories?
body: <p>Querying Healthkit should return an object of array that contains steps, distance and calories.</p>
  
  <p>I was able to return them individually by <br/>
  <strong>Activity Steps/hr</strong></p>
  
  <pre><code>func activitySteps(_ startDate:NSDate, endDate:NSDate, anchorDate:NSDate, completion: @escaping (Array&lt;NSObject&gt;, NSError?) -&gt; ()) {
      let type = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)
      let interval = NSDateComponents()
      interval.hour = 1
  
      let predicate = HKQuery.predicateForSamples(withStart: startDate as Date, end: endDate as Date, options: .strictEndDate)
      let query = HKStatisticsCollectionQuery(quantityType: type!, quantitySamplePredicate: predicate, options: [.cumulativeSum], anchorDate: anchorDate as Date, intervalComponents:interval as DateComponents)
  
      query.initialResultsHandler = { query, results, error in
        if let myResults = results{
          var stepsArray: [NSObject] = []
          myResults.enumerateStatistics(from: startDate as Date, to: endDate as Date) {
            statistics, stop in
  
            if let quantity = statistics.sumQuantity() {
              let steps = quantity.doubleValue(for: HKUnit.count())
  //            print("\(statistics.endDate.timeIntervalSince1970): steps = \(steps)")
  
              let ret =  [
                "steps": steps,
                "startDate" : statistics.startDate.timeIntervalSince1970 * 1000.0,
                "endDate": statistics.endDate.timeIntervalSince1970 * 1000.0
                ] as [String : Any]
              stepsArray.append(ret as NSObject)
            }
          }
          completion(stepsArray, error as NSError?)
        }
      }
  
      healthKitStore.execute(query)
    }
  </code></pre>
  
  <p><br/><strong>Activity Distance/hr</strong></p>
  
  <pre><code>func activityDistance(_ startDate:NSDate, endDate:NSDate, anchorDate:NSDate, completion: @escaping (Array&lt;NSObject&gt;, NSError?) -&gt; ()) {
      let type = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.distanceWalkingRunning)
      let interval = NSDateComponents()
      interval.hour = 1
  
      let predicate = HKQuery.predicateForSamples(withStart: startDate as Date, end: endDate as Date, options: .strictEndDate)
      let query = HKStatisticsCollectionQuery(quantityType: type!, quantitySamplePredicate: predicate, options: [.cumulativeSum], anchorDate: anchorDate as Date, intervalComponents:interval as DateComponents)
  
      query.initialResultsHandler = { query, results, error in
        if let myResults = results{
          var distanceArray: [NSObject] = []
          myResults.enumerateStatistics(from: startDate as Date, to: endDate as Date) {
            statistics, stop in
  
            if let quantity = statistics.sumQuantity() {
              let distance = quantity.doubleValue(for: HKUnit.meter())
              print("\(statistics.endDate.timeIntervalSince1970): distance = \(distance)")
  
              let ret =  [
                "distance": distance,
                "startDate" : statistics.startDate.timeIntervalSince1970 * 1000.0,
                "endDate": statistics.endDate.timeIntervalSince1970 * 1000.0
                ] as [String : Any]
              distanceArray.append(ret as NSObject)
            }
          }
          completion(distanceArray, error as NSError?)
        }
      }
  
      healthKitStore.execute(query)
    }
  </code></pre>
  
  <p><strong>Activity Calories/hr</strong></p>
  
  <pre><code>func activityCalories(_ startDate:NSDate, endDate:NSDate, anchorDate:NSDate, completion: @escaping (Array&lt;NSObject&gt;, NSError?) -&gt; ()) {
      let type = HKSampleType.quantityType(forIdentifier: HKQuantityTypeIdentifier.activeEnergyBurned)
      let interval = NSDateComponents()
      interval.hour = 1
  
      let predicate = HKQuery.predicateForSamples(withStart: startDate as Date, end: endDate as Date, options: .strictEndDate)
      let query = HKStatisticsCollectionQuery(quantityType: type!, quantitySamplePredicate: predicate, options: [.cumulativeSum], anchorDate: anchorDate as Date, intervalComponents:interval as DateComponents)
  
      query.initialResultsHandler = { query, results, error in
        if let myResults = results{
          var caloriesArray: [NSObject] = []
          myResults.enumerateStatistics(from: startDate as Date, to: endDate as Date) {
            statistics, stop in
  
            if let quantity = statistics.sumQuantity() {
              let calories = quantity.doubleValue(for: HKUnit.kilocalorie())
              print("\(statistics.endDate.timeIntervalSince1970): calories = \(calories)")
  
              let ret =  [
                "calories": calories,
                "startDate" : statistics.startDate.timeIntervalSince1970 * 1000.0,
                "endDate": statistics.endDate.timeIntervalSince1970 * 1000.0
                ] as [String : Any]
              caloriesArray.append(ret as NSObject)
            }
          }
          completion(caloriesArray, error as NSError?)
        }
      }
  
      healthKitStore.execute(query)
    }
  </code></pre>
  
  <p>I would instead like to combine all these into a single object. </p>
  
  <pre><code> [
      {
           steps: 70,
           distance:10,
           calories: 200,
           startDate: ...,
           endDate: ...,
       },...
   ]
  </code></pre>
  
  <p>Is it possible to generate above format data with an hourly interval rate directly from HealthKit itself?
  If it isn't possible how can I combine all 3 data into a single object where data can range from single day- week- month.</p>