title: iOS : Healthkit get Oxygen Saturation in real time
body: <p>I'm new to HealthKit framework and wants real time oxygen saturation in blood stream.</p>
  
  <p><strong>REF :</strong> <a href="https://github.com/coolioxlr/watchOS-2-heartrate" rel="nofollow noreferrer">https://github.com/coolioxlr/watchOS-2-heartrate</a></p>
  
  <p>FROM THIS REPO i have tried to make query for oxygen saturation</p>
  
  <pre><code>   func createOxygenSaturationStreamingQuery(_ workoutStartDate: Date) -&gt; HKQuery? {
  
          guard let quantityType = HKObjectType.quantityType(forIdentifier: .oxygenSaturation) else { return nil }
          let datePredicate = HKQuery.predicateForSamples(withStart: workoutStartDate, end: nil, options: .strictEndDate )
  
          let predicate = NSCompoundPredicate(andPredicateWithSubpredicates:[datePredicate])
  
          let oxygenSaturationQuery = HKAnchoredObjectQuery(type: quantityType,
                                                         predicate: predicate,
                                                         anchor: nil,
                                                         limit: Int(HKObjectQueryNoLimit)) { (query, sampleObjects, deletedObjects, newAnchor, error) -&gt; Void in
  
              self.updateOxygenSaturation(sampleObjects)
          }
  
          oxygenSaturationQuery.updateHandler = {(query, samples, deleteObjects, newAnchor, error) -&gt; Void in
              self.updateOxygenSaturation(samples)
          }
          return oxygenSaturationQuery
      }
  
      func updateOxygenSaturation(_ samples: [HKSample]?) {
  
          guard let oxygenSaturationSamples = samples as? [HKQuantitySample] else {return}
  
          DispatchQueue.main.async {
              // RETURN FROM HERE AS EMPTY ARRAY
              guard let sample = oxygenSaturationSamples.first else { return }
              let value = sample.quantity.doubleValue(for: HKUnit(from: "%/min"))
              let stringValue = String(UInt16(value))
              self.label.setText(stringValue)
              // retrieve source from sample
              let name = sample.sourceRevision.source.name
              print("sample.sourceRevision.source.name : \(name)")
              print("PERCENT : \(stringValue)")
          }
      }
  </code></pre>
  
  <p><strong>P.S : I'M CHECKING IN SIMULATOR</strong></p>
  
  <p>I'm getting Heart Rate from that repo, but getting oxygen level empty?</p>
  
  <p>Let me know what wrong i have done and how can i correct?</p>