How to make HKworkoutsession always an active workout session?

Hi,



I'm working on apple watch app and I'm using HKworkoutsession to access heart rate data sample.



In the newest watchos2 beta3 release bug "During an active workout session, new heart rate samples are not generated when the screen is off." is fixed.



My question is how could I set my HKworkoutsession always as an "active work out session" thus I could keep getting heart rate sample as I need.



Thanks

The text means active as in running/on. So once you've created your HKWorkoutSession you can start it and it will be active until you tell it to stop.


To get the heart rate samples though you will need to have a query for the samples.


This project will give you a good overview combined with the WWDC HealthKit videos:


https://github.com/coolioxlr/watchOS-2-heartrate?utm_campaign=This%2BWeek%2Bin%2BSwift&utm_medium=web&utm_source=This_Week_in_Swift_43

@MetzoPaino's link is a good place to get started.


I've observed the following behavior:


* Create and register and HKAnchoredObjectQuery to recieve heart rate samples

* Create and start a workoutsession.


* if your app is displayed on the watch, you will recieve HKSamples of heart rate data as it arrives

* the the screen times out your code will be suspended.

* Once your app becomes active again (on wrist raise) youre updateHandler will recieve a set of samples of all of the heartrate data that was collected while it was "inactive"


This is not ideal for any sort of realtime processing of heart rate data.


Also, very disappointingly the apple watch only updates the heartrate data every 5 seconds or so.


Best of luck!

How to make HKworkoutsession always an active workout session?
 
 
Q