title: HKWorkoutSession is not running consistently
body: <p>in watchOS, i am just trying to get HR value in real-time while a workoutSession is running.</p>
  
  <pre><code>func startHeartRateQuery(updateHandler: @escaping ([HKQuantitySample]?) -&gt; Void) {
      guard let quantityType = HKObjectType.quantityType(forIdentifier: .heartRate) else {
          return
      }
  
      let heartRateQuery = HKAnchoredObjectQuery(type: quantityType, predicate: nil, anchor: anchor, limit: Int(HKObjectQueryNoLimit)) { (query, sampleObjects, deletedObjects, newAnchor, error) -&gt; Void in
          guard let newAnchor = newAnchor else {return}
          self.anchor = newAnchor
          updateHandler(sampleObjects as? [HKQuantitySample])
      }
  
      heartRateQuery.updateHandler = {(query, samples, deleteObjects, newAnchor, error) -&gt; Void in
          self.anchor = newAnchor!
          updateHandler(samples as? [HKQuantitySample])
      }
  
      healthStore.execute(heartRateQuery)
      activeDataQueries.append(heartRateQuery)
  }
  </code></pre>
  
  <p>And This is how i do start workoutSession.</p>
  
  <pre><code>func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any] = [:]) {
      guard let d = delegate else {
          return
      }
  
      if let u = userInfo["status"] as? String {
          d.receivedWorkoutRunningStatus(u)
      }
  
      if let c = userInfo["clock"] as? String {
          d.receiveWorkoutRestPeriodClock(c)
      }
  }
  </code></pre>
  
  <p>You can find receivedWorkoutRunningStatus function on InterfaceController.
  This is InterfaceController.swift which is first screen on Watch App.</p>
  
  <pre><code>class InterfaceController: WKInterfaceController, HKWorkoutSessionDelegate {
  
  @IBOutlet var lblHeartRate: WKInterfaceLabel!
  @IBOutlet var lblSplitIntervalNumber: WKInterfaceLabel!
  @IBOutlet var lblRestPeriodClock: WKInterfaceLabel!
  
  private let healthStoreManager = WatchHealthKitManager()
  private let parentConnector = ParentConnector()
  private var workoutSession: HKWorkoutSession!
  
  override func awake(withContext context: Any?) {
      super.awake(withContext: context)
  
      // Create a workout session with the workout configuration
      do {
          let workoutConfiguration = HKWorkoutConfiguration()
          workoutConfiguration.locationType = .indoor
          workoutConfiguration.activityType = .rowing
  
          workoutSession = try HKWorkoutSession(configuration: workoutConfiguration)
      } catch {
          fatalError(error.localizedDescription)
      }
  
      //// initial setup
      parentConnector.activate()
      parentConnector.delegate = self
      workoutSession.delegate = self
  }    
  // MARK: - Data Accumulation
  
  private func startAccumulatingData() {
      healthStoreManager.startHeartRateQuery() { quantitySamples in
          DispatchQueue.main.async {
              guard !self.isPaused() else {
                  return
              }
  
              guard let heartRateSamples = quantitySamples else {
                  return
              }
  
              let hrUnit = HKUnit(from: "count/min")
              guard let sample = heartRateSamples.first else {
                  return
              }
  
              let value = sample.quantity.doubleValue(for: hrUnit)
              self.updateHeartRate(value: value)
              self.parentConnector.transfer(value: value)
          }
      }
  }
  
  func workoutSession(_ workoutSession: HKWorkoutSession,
                      didChangeTo toState: HKWorkoutSessionState,
                      from fromState: HKWorkoutSessionState,
                      date: Date) {
      switch (toState) {
      case .running:
          startAccumulatingData()
      case .ended:
          stopAccumulatingData()
      default:
          print("Error")
      }
  }
  func receivedWorkoutRunningStatus(_ status: String) {
      if (status == "Start") {
          healthStoreManager.start(workoutSession)
      } else if (status == "Finish") {
          healthStoreManager.end(workoutSession)
          lblHeartRate.setText("No Active Workout")
      }
  
      DispatchQueue.main.async {
          self.lblSplitIntervalNumber.setText(status)
      }
  }
  </code></pre>
  
  <p>On the iPhone App, I send a "Start" string using transferUserInfo function to trigger the workoutSession beginning. This is not working properly, it only sometimes works, it is very inconsistent.</p>
  
  <p>I would appreciate if you have any advice or alternative approaches.</p>
  
  <p>Thank you in advance.</p>