Reputation: 1458
Im finding that asking for a sum of, for example, dietary calories from HealthKit will return a value which automatically subtracts entries made from my own app. That is great and is the behaviour I want, but this doesn't happen all the time - sometimes it includes entries I have made from my own app. This makes it impossible to use the HealthKit statistics query results sensibly without going through each object.
predicate = [HKQuery predicateForSamplesWithStartDate:[date dateByAddingTimeInterval:-queryLatencyPeriod] endDate:nil options:0];
HKStatisticsQuery *sumQuery;
sumQuery = [[HKStatisticsQuery alloc] initWithQuantityType:[HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierDietaryEnergyConsumed] quantitySamplePredicate:predicate options:HKStatisticsOptionCumulativeSum completionHandler:^(HKStatisticsQuery *query, HKStatistics *result, NSError *error) {
float calories = [[result sumQuantity] doubleValueForUnit:[HKUnit calorieUnit]];
}];
Sometimes this includes my app's entries in the total, sometimes it doesn't. Simple as that. I think its an error in HealthKit.
Upvotes: 2
Views: 1365
Reputation: 7353
Statistics queries aggregate samples from multiple sources. When samples of cumulative types from two different sources overlap in time, HealthKit chooses one source to use. The priority of a source is determined per sample type by the order in which they are listed in the in the Data Sources section of the table on the Share Data view for that sample type in the Health app.
I suspect that when you see that samples from your own app are not being counted, it is actually because samples from another source overlap them.
Upvotes: 6