title: HealthKit fetch data between interval
body: <p>I have a little problem grasping HealthKit. I want to get heart rate from HealthKit with specific time.
   I have done this in the past (until I noticed that I couldn't fetch data when the phone was locked)</p>
  
  <pre><code> func retrieveMostRecentHeartRateSample(completionHandler: (sample: HKQuantitySample) -&gt; Void) {
      let sampleType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)
      let predicate = HKQuery.predicateForSamplesWithStartDate(NSDate.distantPast() as! NSDate, endDate: NSDate(), options: HKQueryOptions.None)
      let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
  
      let query = HKSampleQuery(sampleType: sampleType, predicate: predicate, limit: 1, sortDescriptors: [sortDescriptor])
          { (query, results, error) in
              if error != nil {
                  println("An error has occured with the following description: \(error.localizedDescription)")
              } else {
                  let mostRecentSample = results[0] as! HKQuantitySample
                  completionHandler(sample: mostRecentSample)
              }
          }
      healthKitStore.executeQuery(query)
  }
  
  var observeQuery: HKObserverQuery!
  
  func startObservingForHeartRateSamples() {
      println("startObservingForHeartRateSamples")
      let sampleType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)
  
      if observeQuery != nil {
          healthKitStore.stopQuery(observeQuery)
      }
  
      observeQuery = HKObserverQuery(sampleType: sampleType, predicate: nil) {
          (query, completionHandler, error) in
  
          if error != nil {
              println("An error has occured with the following description: \(error.localizedDescription)")
          } else {
             self.retrieveMostRecentHeartRateSample {
                  (sample) in
                  dispatch_async(dispatch_get_main_queue()) {
                  let result = sample
                  let quantity = result.quantity
                  let count = quantity.doubleValueForUnit(HKUnit(fromString: "count/min"))
                  println("sample: \(count)")
                  heartChartDelegate?.updateChartWith(count)
                  }
              }
          }
      }
      healthKitStore.executeQuery(observeQuery)
  }
  </code></pre>
  
  <p>This code will fetch the latest sample every time it is a change in HealthKit. But as I said earlier, it won't update when the phone is locked. I tried using:    </p>
  
  <pre><code>self.healthKitStore.enableBackgroundDeliveryForType(HKQuantityType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate), frequency: HKUpdateFrequency.Immediate) { (success, error) in
  if success{
     println("success")
  } else {
     println("fail")
  }            
  </code></pre>
  
  <p>}</p>
  
  <p>But this didn't work and as I found out there was a bug that Apple said it wasn't working as they wanted. Guess it is some security-thing.</p>
  
  <p>But then I thought, maybe I can request samples between a startTime and endTime. For example i have <strong>EndTime(2015-05-31 10:34:45 +0000)</strong> and <strong>StartTime(2015-05-31 10:34:35 +0000)</strong>.
  So my question is how can I get heart rate samples between these two times. </p>
  
  <p>I guess I must do it in the</p>
  
  <pre><code>HKQuery.predicateForSamplesWithStartDate(myStartTime, endDate: myEndTime, options: HKQueryOptions.None)
  </code></pre>
  
  <p>But when I tried it didn't find anything. Maybe I got this all wrong...</p>
  
  <p>I am using a heart rate monitor on my chest and I know that I get some values in healthKit within the start and end time. </p>
  
  <p>Edit:</p>
  
  <p>Ok I tried it and it is working some times, not always. Someone has an idea?</p>
  
  <pre><code>func fetchHeartRates(endTime: NSDate, startTime: NSDate){
      let sampleType = HKObjectType.quantityTypeForIdentifier(HKQuantityTypeIdentifierHeartRate)
      let predicate = HKQuery.predicateForSamplesWithStartDate(startTime, endDate: endTime, options: HKQueryOptions.None)
      let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
  
      let query = HKSampleQuery(sampleType: sampleType, predicate: predicate, limit: 100, sortDescriptors: [sortDescriptor])
          { (query, results, error) in
              if error != nil {
                  println("An error has occured with the following description: \(error.localizedDescription)")
              } else {
                  for r in results{
                      let result = r as! HKQuantitySample
                      let quantity = result.quantity
                      let count = quantity.doubleValueForUnit(HKUnit(fromString: "count/min"))
                      println("sample: \(count) : \(result)")
                  }
              }
      }
      healthKitStore.executeQuery(query)
  }
  </code></pre>
  
  <p><strong>Edit 2</strong>:</p>
  
  <p>It was working but I couldn't call it the way I did. So I fetched it a couple of seconds later and it worked fine :)</p>