Skip to content

Instantly share code, notes, and snippets.

@eriadam
Created March 1, 2018 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eriadam/5f5388c1ceb5f23681f8dd44a125bf06 to your computer and use it in GitHub Desktop.
Save eriadam/5f5388c1ceb5f23681f8dd44a125bf06 to your computer and use it in GitHub Desktop.
/// When the completion is called, an other query is executed
/// to fetch the latest heart rate
self.fetchLatestHeartRateSample(completion: { sample in
guard let sample = sample else {
return
}
/// The completion in called on a background thread, but we
/// need to update the UI on the main.
DispatchQueue.main.async {
/// Converting the heart rate to bpm
let heartRateUnit = HKUnit(from: "count/min")
let heartRate = sample
.quantity
.doubleValue(for: heartRateUnit)
/// Updating the UI with the retrieved value
self?.heartRateLabel.setText("\(Int(heartRate))")
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment