title: HKObserverQuery in Background mode
body: <p>I have an application that need to track user heart rate readings from apple watch, so I did all the required steps that I found on apple guides, and here is the code that I am using:</p>
  
  <pre><code>static var query: HKObserverQuery?
  
      func startObservingHeartRate() {
  
          guard let heartRateSampleType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate) else {
  
              fatalError("Unable to create a step count sample type")
  
          }
  
  
  
          AppDelegate.query = HKObserverQuery(sampleType: heartRateSampleType, predicate: nil, updateHandler: { (query, completionHandler, error) in
  
                  if error != nil {
  
                      // Perform Proper Error Handling Here...
                      print("An error occured while setting up the Heart Rate observer.")
  
                  }
  
                  //Read the last strored heatt rate in add it to the DB
                  //Add last fetched Heart Rate reading to DB and send it to clips
                  HealthKitManager().fetchLastStoredHeartRate(completion: { (lastReading, error) in
  
                      guard let lastReading = lastReading else {
  
                          //There is no heart readings in HealthKit
                          return
  
                      }
  
                      //Check if Last HR value is Abnormal
                      if lastReading.doubleValue &gt; 60 {
  
                          //TODO: - Schedule notification
                          if UIApplication.shared.applicationState == .background {
  
  
  
                          } else {
  
                          //TODO: - Show popup to the user
  
                          }
  
                      }
  
                  })
  
                  completionHandler()
  
          })
  
          healthKitStore.execute(AppDelegate.query!)
  
          configureHeartRateObserver()
  
      }
  
      func configureHeartRateObserver() {
  
          guard let heartRateSampleType = HKObjectType.quantityType(forIdentifier: HKQuantityTypeIdentifier.heartRate) else {
  
              fatalError("Unable to create a step count sample type")
  
          }   
  
          healthKitStore.enableBackgroundDelivery(for: heartRateSampleType, frequency: HKUpdateFrequency.immediate) { (success, error) in
  
              if success {
  
                  print("Enabled background delivery of Heart Rate changes")
  
              } else {
  
                  print("Failed to enable background delivery of weight changes. ")
  
              }
  
          }
  
      }
  </code></pre>
  
  <p>and I am calling "startObservingHeartRate" in didFinishLaunchingWithOptions in AppDelegate, assuming that this query should be executed once a new reading added or deleted from the health kit store, every thing is fine, if app is in background or killed the handler wake up my app and it do the updates.</p>
  
  <p>But whenever I put the app in background then put it in foreground again it execute the observer query for many times even if there is no new readings added to the HealthKit store and in this case I am getting the same last heart rate for many times for no reason.</p>
  
  <p>Please any recommendation on how to use this types of query or any changes I need to do with my current implementation.</p>