Apple Watch App: Displaying The Heart Rate

Apple Watch, fitness app, current heart rate (bpm) on the screen. Simple task. Or is it? Tl;dr: full example here.

First off, an observer has to be created, so updates are received whenever HealthKit’s heart rate data changes. This can be done using the HKObserverQuery after creating a sample type for the heart rate.

The weird thing is — for me at least — that this does not actually tell you the heart rate. Another query has to be executed in order to retrieve the latest available value. That query needs a sample type, predicates to define the date interval and sorting to get the latest one. I used a completion closure so we can return the fetched value.

And we are still not done… The returned data is no way displayable, it has to be converted using a HKUnit of “count/min”.

See full example here.

I mean, it does work, but that’s 80+ lines of code for a single number.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.