1

What is the correct way to use background delivery in iOS 8 HealthKit?

The following is my code to enable HealthKit background delivery.

- (void)observeSleepData {
    HKCategoryType *sleepType = [HKCategoryType categoryTypeForIdentifier:HKCategoryTypeIdentifierSleepAnalysis];
    [self.healthStore enableBackgroundDeliveryForType:sleepType frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error) {
        if (success) {

        }
    }];
    HKObserverQuery *query = [[HKObserverQuery alloc] initWithSampleType:sleepType predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error) {
        NSLog(@"sleep data updated");
    }];
    [self.healthStore executeQuery:query];
}

Do I need to configure extra things to make this work other than enabling HealthKit capability? Does HKObserverQuery respond to manual input in iOS8 native Health app?

pkamb
  • 33,281
  • 23
  • 160
  • 191
YoungLion
  • 11
  • 2
  • 6

2 Answers2

2

From my own limited testing, a response from HKObserverQuery only means something changed in the data type you specified. It doesn't tell you what changed or come back with new data. Your code above should work, as long as you put an actual query (likely an anchored query) in the if (success) {} block.

I've gotten code similar to this to run when I update the native Health app.

EDIT:

I gave a more complete answer here: https://stackoverflow.com/a/26385281/1563787

Community
  • 1
  • 1
drdaanger
  • 350
  • 1
  • 10
  • this is correct. Once you're notified of the change, you must query for the data that you're interested in. Finally, you must call the completion function IOS gives to you in the callback to notify it that you're finished handling the data. – cmollis Mar 05 '15 at 14:32
2

The HealthKit API Reference says:

The HealthKit data is only kept locally on the user’s device. For security, the HealthKit store is encrypted when the device is locked. The HealthKit store can only be accessed by an authorized app. As a result, you may not be able to read data from the store when your app is launched in the background; however, apps can still write data to the store, even when the phone is locked. HealthKit temporarily caches the data and saves it to the encrypted store as soon as the phone is unlocked

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Maxwell
  • 91
  • 1
  • 9
  • Is that a direct quote? If so, you can indicate it by prefixing the quoted lines with `>` - see the [editing help](/editing-help#simple-blockquotes). If not, you might want to make it clearer that you're paraphrasing. – Toby Speight Nov 01 '16 at 08:31
  • Yes.The official document. You could find it in app developer. Link: https://developer.apple.com/reference/healthkit – Maxwell Nov 08 '16 at 09:28
  • 1
    I've edited your answer to indicate that it's a direct quote. In future, you should be able to do this yourself - see [Help: Editing](/editing-help). – Toby Speight Nov 08 '16 at 10:38