Running HealthKit queries in the background

Recently I’ve been trying to use HKObserver to monitor queries from HealthKit in the background. I wrote a simple integration where my app would print to the console every time a new workout was recorded. However, it was never triggered, except when I opened my app in the foreground.

I spent more time than I’d like to admit on StackOverflow and Apple’s developer forums trying to figure this out. What on Earth was I doing wrong? I played around with my app’s background entitlements, I set breakpoints all over the place, I thought I’d tried everything.

Eventually I found the answer, and it’s a very simple one. I didn’t have background app refresh enabled on my phone 🤦‍♂️. Enable that and it’ll work fine.

PS: There is some confusion as to whether your app needs to explicitly use the background fetch entitlement to use HealthKit in the background. I tried both and you don’t need the entitlement.

Thanks for reading.

To get in touch, email me or find me on Mastodon or Twitter.

If you liked this post, you'll love my iOS apps. Check them out below.

Personal Best

Level up your workouts

Taylor's Version

Upgrade Taylor Swift songs in your playlists

SalaryPig

Meet Trevor, the salary-tracking pig

Taylor Swift Quiz

How well do you know Taylor Swift?