Instance Method

enableBackgroundDelivery(for:frequency:withCompletion:)

Enables the background delivery of updates.

Declaration

func enableBackgroundDelivery(for type: HKObjectType, frequency: HKUpdateFrequency, withCompletion completion: @escaping (Bool, Error?) -> Void)

Parameters

type

The type of data to observe. This object can be any concrete subclass of the HKObjectType class (any of the classes HKCharacteristicType , HKQuantityType, HKCategoryType, HKWorkoutType or HKCorrelationType).

frequency

The maximum frequency of the updates. The system wakes your app from the background at most once per time period specified. For a complete list of valid frequencies, see HKUpdateFrequency.

completion

A block that this method calls as soon as the background delivery is enabled. This block is passed the following parameters:

success

A Boolean value. This parameter contains true if the background delivery was successfully enabled; otherwise, false.

error

An error object. If an error occurred, this object contains information about the error; otherwise, it is set to nil.

Discussion

Call this method to register your app for background updates. HealthKit wakes your app whenever new samples of the specified type are saved to the store. Your app is called at most once per time period defined by the specified frequency.

As soon as your app launches, HealthKit calls the update handler for any observer queries that match the newly saved data. If you plan on supporting background delivery, set up all your observer queries in your app delegate’s application(_:didFinishLaunchingWithOptions:) method. By setting up the queries in application(_:didFinishLaunchingWithOptions:), you ensure that the queries are instantiated and ready to use before HealthKit delivers the updates.

After your observer queries have finished processing the new data, you must call the update’s completion handler. This lets HealthKit know that you have successfully received the background delivery. If you do not call the update’s completion handler, HealthKit continues to attempt to launch your app using a backoff algorithm. If your app fails to respond three times, HealthKit assume your app cannot receive data and stops sending you background updates.

For more information on the background delivery completion handler, see isHealthDataAvailable().

See Also

Managing Background Delivery

enum HKUpdateFrequency

Constants that determine how often the system will launch your app in response to changes to HealthKit data.

func disableBackgroundDelivery(for: HKObjectType, withCompletion: (Bool, Error?) -> Void)

Disables background deliveries of update notifications for the specified data type.

func disableAllBackgroundDelivery(completion: (Bool, Error?) -> Void)

Disables all background deliveries of update notifications.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software