Reputation: 395
I've registered for background delivery of four types of HealthKit data in my delegate's didFinishLaunching
method. The data types are steps, sleep, workouts, and energyConsumed
.
I've noticed my observer queries for these data types are being called by HealthKit/iOS many, many times a second in some cases. Here's an example from some device logs of the sleep observer query being called:
2017/03/27 07:21:58:821 Delegate - steps observer query received new data
2017/03/27 07:21:58:894 Delegate - steps observer query received new data
2017/03/27 07:21:58:936 Delegate - steps observer query received new data
2017/03/27 07:21:58:973 Delegate - steps observer query received new data
2017/03/27 07:21:58:993 Delegate - steps observer query received new data
2017/03/27 07:21:59:000 Delegate - steps observer query received new data
2017/03/27 07:21:59:024 Delegate - steps observer query received new data
2017/03/27 07:21:59:130 Delegate - steps observer query received new data
2017/03/27 07:21:59:145 Delegate - steps observer query received new data
2017/03/27 07:21:59:156 Delegate - steps observer query received new data
2017/03/27 07:21:59:169 Delegate - steps observer query received new data
2017/03/27 07:21:59:309 Delegate - steps observer query received new data
2017/03/27 07:21:59:328 Delegate - steps observer query received new data
2017/03/27 07:21:59:346 Delegate - steps observer query received new data
2017/03/27 07:21:59:404 Delegate - steps observer query received new data
2017/03/27 07:21:59:480 Delegate - steps observer query received new data
2017/03/27 07:21:59:499 Delegate - steps observer query received new data
2017/03/27 07:21:59:520 Delegate - steps observer query received new data
2017/03/27 07:21:59:547 Delegate - steps observer query received new data
2017/03/27 07:21:59:561 Delegate - steps observer query received new data
2017/03/27 07:21:59:571 Delegate - steps observer query received new data
2017/03/27 07:21:59:583 Delegate - steps observer query received new data
Here's the code in my app delegate where this log is created:
- (void)setUpStepsObserverQuery {
__weak typeof(self) weakSelf = self;
HKSampleType *sampleType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
HKObserverQuery *query = [[HKObserverQuery alloc] initWithSampleType:sampleType predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error) {
if (completionHandler) {
completionHandler();
}
if (error) {
DDLogDebug(@"App Delegate - An error occured while setting up the stepCount observer: %@", error.localizedDescription);
} else {
DDLogDebug(@"Delegate - steps observer query received new data");
weakSelf.activityTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[weakSelf endBackgroundTask:weakSelf.activityTask];
}];
// At this point I run an anchor query to check if there really is new data, and if there is I go ahead and send the new HealthKit data to our server.
}];
[_healthStore executeQuery:query]; // _healthStore is a shared instance of HKHealthStore I created earlier
}
This is how I enable background delivery for steps data. I call this method in didFinishLaunching
and it calls the method I've shared a snippet of above, setUpStepsObserverQuery
if ([defaults boolForKey:HK_ACTIVITY_SYNC]) {
[self setUpStepsObserverQuery];
[_healthStore enableBackgroundDeliveryForType:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]
frequency:HKUpdateFrequencyImmediate
withCompletion:^(BOOL success, NSError * _Nullable error) {
DDLogDebug(@"Delegate - enabled step count background updates");
}];
Any ideas on why HealthKit would be calling my observer queries so many times in the same minute?
Upvotes: 1
Views: 1043
Reputation: 855
This is because every time your observe query gets called, your didFinishLaunching
function is also called, and enableBackgroundDeliveryForType
gets also called and each time it's enabled, the query is being performed.
you have to check if background delivery is enabled, don't enable it again!
Upvotes: 3
Reputation: 7353
This might be a bug rather than expected behavior, so you should file a radar with Apple.
You could also try specifying less frequent HKUpdateFrequency
such as HKUpdateFrequencyHourly
. As mentioned in the documentation for HKHealthStore
, the system is actually intended to have a minimum frequency of hourly for high volume sample types such as steps and active energy.
Upvotes: 0