I am building a watchOS app with iOS companion app.
The watch app needs to track the heart rate during the night or while user is sleeping. And the desired frequency of measurement is 0.2Hz (every 5 seconds)
For this I am using the HKWorkout mode with mindAndBody session.
While it works fine, One of the main issue is: after about 6-7 hours of usage, the battery on the watch drains between 40% (Series 9) and 100% (series 7, I think)
My questions:
- Are there any other option to track user's heart rate without workout, while the app could be in background?
- Another side effect of this workout mode is, Even if we choose not to save the workout in HealthKit, the Activity rings gets populated by this mindAndBody session, which makes it when the user is waking up, the bar is already full, This is not desired.
Is there any option to specify for ActivityRing skips this?
Highly appreciate any help in advance.
Cheers - Prakash
How latest information could we get from Healthkit with this approach? We could still relax the frequency as low as 0.033 Hz (every 30 seconds).
You can use HKObserverQuery + enableBackgroundDelivery(for:frequency:withCompletion:) to have your app get notified (even in the background) when the HealthKit data you are observing changes, and your app can then fetch the latest data from there.
I don't have anything to share about how often the system samples the heart rate while the user sleeping, but you can start with grabbing the data from the HealthKit store, and see if that is good enough for your analysis.
Note that the HealthKit data an Apple Watch generates is only available on the paired iPhone after the HealthKit stores on both devices are synchronized, and so the data your iPhone app gets may have a delay because the HealthKit stores don't synchronize in real time.
Best,
——
Ziqiao Chen
Worldwide Developer Relations.