Instance Method

enableBackgroundDeliveryForType:frequency:withCompletion:

Enables the background delivery of updates.

Declaration

- (void)enableBackgroundDeliveryForType:(HKObjectType *)type frequency:(HKUpdateFrequency)frequency withCompletion:(void (^)(BOOL success, NSError *error))completion;

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 YES if the background delivery was successfully enabled; otherwise, NO.

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

HKUpdateFrequency

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

- disableBackgroundDeliveryForType:withCompletion:

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

- disableAllBackgroundDeliveryWithCompletion:

Disables all background deliveries of update notifications.