How to get Apple Watch to send workout session data to my fitness app instantly

I am working with Apple HealthKit, and I need to add functionality to my app that allows a user's Apple Watch to automatically send the relevant data for their workout session (e.g. vo2 max, start timestamp, end timestamp, heartbeat, duration etc) as soon as they end their workout. This would need to work for any type of workout they do.

E.g. if they are doing a running session, when they end their workout my app needs to receive the data for that running session almost instantly.

I know that enableBackgroundDelivery allows my app to listen to changes in Apple Health in the background, but I'm not sure which method in HealthKit will allow me to implement the above use case.

Does anyone have any pointers? I just want to know what methods in HealthKit will allow me to achieve this.

Answered by Frameworks Engineer in 765974022

It would be good to understand more of the use case/need for the immediate syncing... By default data from watch will sync to phone (if it's within range) when the workout completes so you could listen on the phone as well. Yes, enableBackgroundDelivery is the right API to be used. And it needs to be used in conjunction with HKObserverQuery. See the following

https://developer.apple.com/documentation/healthkit/hkobserverquerycompletionhandler

https://developer.apple.com/documentation/healthkit/hkobserverquery/executing_observer_queries

Accepted Answer

It would be good to understand more of the use case/need for the immediate syncing... By default data from watch will sync to phone (if it's within range) when the workout completes so you could listen on the phone as well. Yes, enableBackgroundDelivery is the right API to be used. And it needs to be used in conjunction with HKObserverQuery. See the following

https://developer.apple.com/documentation/healthkit/hkobserverquerycompletionhandler

https://developer.apple.com/documentation/healthkit/hkobserverquery/executing_observer_queries

How to get Apple Watch to send workout session data to my fitness app instantly
 
 
Q