title: How to get Apple health data by date wise?
body: <p><strong>Apple health app gives the data by Date wise as shown in below image.</strong></p>
  
  <p><a href="https://i.stack.imgur.com/ujGOi.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ujGOi.png" alt="enter image description here"></a></p>
  
  <p>By using <code>HealthKit</code> i am fetching the steps data from apple health
  as </p>
  
  <pre><code>let p1 = HKQuery.predicateForSamples(withStart: fromDate, end: Date(), options: .strictStartDate)
  let p2 = HKQuery.predicateForObjects(withMetadataKey: HKMetadataKeyWasUserEntered, operatorType: .notEqualTo, value: true)
  let timeSortDesriptor = NSSortDescriptor(key: HKSampleSortIdentifierEndDate, ascending: false)//as in health kit entry
  let quantityType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
  let predicate = HKQuery.predicateForSamples(withStart: fromDate, end: Date(), options: .strictStartDate)
  let sourceQuery = HKSourceQuery(sampleType: quantityType, samplePredicate: predicate, completionHandler: { query,sources,error in
  
  if sources?.count != 0  &amp;&amp; sources != nil {
  
                  let lastIndex = sources!.count - 1
                  var sourcesArray = Array(sources!)
                  for i in 0..&lt;sourcesArray.count {
                      let sourcePredicate = HKQuery.predicateForObjects(from: sourcesArray[i])
                      let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [p1, p2,sourcePredicate])
                      let query = HKSampleQuery(sampleType: quantityType, predicate: predicate, limit: HKObjectQueryNoLimit, sortDescriptors: [timeSortDesriptor], resultsHandler: { query, results, error in
                          guard let samples = results as? [HKQuantitySample] else {
                              return
                          }......
  </code></pre>
  
  <p>sourceQuery gives multiple object like Apple watch, My iPhone.
  further i am using for loop with <code>HKSampleQuery</code> which gives <code>HKQuantitySample</code> object. 
  Problem is <code>[HKQuantitySample]</code> gives Array of step data which is not sorted datewise. 
  I am looking for data which are club for the date like what apple health show in Health app.</p>
  
  <p>Yes there is workaround like manually sort the data from <code>[HKQuantitySample]</code> by date wise. but there may be workaround by using <code>predicates</code> or something else. Please feel free to ask if you need any extra information.</p>
  
  <p><strong>EDIT: As suggested by @Allan</strong>
  I have added <a href="https://developer.apple.com/documentation/healthkit/hkstatisticscollectionquery" rel="noreferrer">HKStatisticsCollectionQuery</a>, YES it gives data <strong>datewise</strong> but the steps count receiving is not same as in Apple health App. is something Addition/modification required in below code ?</p>
  
  <pre><code>let last10Day = Calendar.current.date(byAdding: .day, value: -10, to: Date())!
          var interval = DateComponents()
          interval.day = 1
          let quantityType1 = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
          // Create the query
          let query = HKStatisticsCollectionQuery(quantityType: quantityType1,
                                                  quantitySamplePredicate: nil,
                                                  options: .cumulativeSum,
                                                  anchorDate: last10Day,
                                                  intervalComponents: interval)
          // Set the results handler
          query.initialResultsHandler = {
              query, results, error in
  
              guard let statsCollection = results else {
                  // Perform proper error handling here
                  fatalError("*** An error occurred while calculating the statistics: \(String(describing: error?.localizedDescription)) ***")
              }
              let endDate = Date()
              statsCollection.enumerateStatistics(from: last10Day, to: endDate, with: { (statistics, stop) in
                  if let quantity = statistics.sumQuantity() {
                      let date = statistics.startDate
                      let value = quantity.doubleValue(for: HKUnit.count())
                      print("--value--&gt;",value, ",--For Date--&gt;",date)
                  }
              })
          }
              healthStore.execute(query)
  </code></pre>