Wake-up detection with HealthKit

Hello HealthKit Experts & Enthusiasts!

I am building an app called one sec which forces people to take a deep breath before they can use social media apps (it’s using Shortcuts Automations for that).

One important feature of one sec is the Good Morning Countdown:

For a specified time after waking up (e.g. 30mins) selected apps are blocked completely. This helps to start the day screen-free.

They way it works is, the user grants access to read HKCategoryTypeIdentifier.sleepAnalysis data.

I have implemented a HKObserverQuery and enableBackgroundDelivery in order to be informed whenever new HKCategoryTypeIdentifier.sleepAnalysis becomes available.

I noticed that when I have my device connected to Xcode, this works as expected. However, when I quit the app and launch it from my Home Screen, my observer query is not informed about new sleep data (except when my app is running in foreground). Any ideas?

Furthermore, I have noticed that sometimes sleep data is provided delayed to HealthKit, many minutes (sometimes even longer) after waking up, no sleep samples are to be found in the Health app. Of course, for my app it is crucial to get accurate + timely so apps can be blocked accordingly.

Is this an issue that the Apple Watch first needs to send the samples to the phone?

Thanks a lot for your help!

@Quappi did you make sure to enable HealthKit Background Delivery in your Signing & Capabilities tab? I don't remember off hand if you try to enable via code and don't have this setting enabled if you get errors or not.

This would be the first thing that I would double check. You should also check the docs about background delivery, not all types deliver in the background immediately.

Wake-up detection with HealthKit
 
 
Q