1

Let's say we're using step count as the trigger- if the user takes 10 steps, I want a notification to pop up saying so.

This question has been answered here: Healthkit background delivery when app is not running

However, I haven't been able to get it to work. If I walk around with the app in the foreground the step count will get updated. If I leave the app in the background the step count only seems to be updated after I reopen the app. So the background query doesn't seem to be working.

I've tried enabling Background Modes, no effect; I've tried leaving the app in the background for several hours thinking it might take a while for the data to be sent- nothing.

Someone elsewhere was saying to try StatisticsCollectionQuery because StatisticsQuery was broken- any truth to that? StatisticsCollectionQuery is not really ideal for what I'm doing.

Here's my query code:

[self.healthStore enableBackgroundDeliveryForType:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]
                                        frequency:HKUpdateFrequencyImmediate
                                   withCompletion:^(BOOL success, NSError * _Nullable error) {
                                       if (error) {

                                           // Perform Proper Error Handling Here...
                                           NSLog(@"*** An error occured while setting up background updates. %@ ***",
                                                 error.localizedDescription);
                                       }
                                       if (success == YES) {
                                           backgroundEnabled = YES;
                                       }
                                   }];


// set up running observer

HKSampleType *sampleType =
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];

HKObserverQuery *query =
[[HKObserverQuery alloc]
 initWithSampleType:sampleType
 predicate:nil
 updateHandler:^(HKObserverQuery *query,
                 HKObserverQueryCompletionHandler completionHandler,
                 NSError *error) {

     if (error) {

         // Perform Proper Error Handling Here...
         NSLog(@"*** An error occured while setting up the stepCount observer. %@ ***",
               error.localizedDescription);
         // Error popup

         return;
     }

     // Take whatever steps are necessary to update your app's data and UI
     // This may involve executing other queries
     [self updateDailyStepCount];

     // If you have subscribed for background updates you must call the completion handler here.
     completionHandler();

 }];

[self.healthStore executeQuery:query];
Community
  • 1
  • 1
Adam
  • 1,486
  • 3
  • 20
  • 35

1 Answers1

2

The fact that you requested HKUpdateFrequencyImmediate suggests you may not have carefully studied the docs for enableBackgroundDeliveryForType:frequency:withCompletion:. The docs have a large "Note" box explaining that step count will never be provided more often then hourly.

If that's the case, I suggest closely reading the Discussion in that section, because HK is tricky and non-obvious. For example, are you calling this query in application:didFinishLaunchingWithOptions:? Have you tried reinstalling your app (I would delete and reinstall from scratch). If you don't call the completionHandler() three times, iOS will stop notifying you. I recommend building a small, simple app that does nothing but log the step count; use that to work through all the details.

See also HealthKit (iOS) wont deliver data in background (objC). It's unclear if you've already implemented these points from your discussion.

Community
  • 1
  • 1
Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • I used HKUpdateFrequencyImmediate just to get the fastest result possible, but I know it will default to hourly, I can try changing that. I am calling the query in application:didFinishLaunchingWithOptions. I read the discussion, it says you have to call completionHandler once, and I did that (the three times is something else). – Adam Apr 20 '17 at 20:37
  • Sorry, just meant by 3 times, if iOS asks you three times and you don't respond, then it stops asking you. But you seem on top of that already. – Rob Napier Apr 21 '17 at 00:50