WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

HealthKit Async enableBackgroundDelivery method

Forums > iOS

Hi all!

I'm looking for some advice and extra details on using the async version of enableBackgroundDelivery in the below documentation. For better or worse, the documentation only tells me the async version exists but not much more about how to use it.

The big thing I'm getting hung up on is after enabling background delivery, where do I tell Swift to run the hkquery after background updates are detected?

Apple Documentation: enableBackgroundDelivery

Extra notes:

  • My app uses exclusively SwiftUI targeting iOS 16
  • My HKHealthStore is not currently setup as an ObservableObject. But it easily can be, let me know if that would help?
  • My app only reads data currently, there isn't a need currently to write data back to healthkit. Partially related, it also doesn't need to know about deleted data either.

3      

From my understanding you call it after creating your HKHealthStore object on this created object. You don't need to call it yourself afterwards. This is handled automatically. You only set the update frequency. If you have more than one type you have to call it for every type.

3      

That is true, it does only need to be called at the HKHealthStore initialization. I'm thinking its a good idea to call it after the user grants HealthKit access as well.

I happened to find out in the last week how to use the new data found in the background. Here is a stackoverflow question and answer with the exact code needed to authorize healthkit and use the new data found.

How to query for HealthKit (HKWorkout) updates when app is in background?

Health data is protected when the device is locked though so I may not be able to do much more in the background than send a notification that new health data exists.

3      

Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.