LAST CHANCE: 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      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until July 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

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.