My app uses the HealthKit framework to retrieve user health data. I want to get around 25 different data points from HealthKit.
To do this, I currently have the 25 calls in a for-loop
inside the completion handler for the sample query. Is there any way to combine the results, or do this process more efficiently?.
To my knowledge, this is how I have to do it (see code below). Thank you in advance.
NSDate *startDate, *endDate;
// Use the sample type for step count
HKSampleType *sampleType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
// Create a predicate to set start/end date bounds of the query
NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:endDate options:HKQueryOptionStrictStartDate];
// Create a sort descriptor for sorting by start date
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:YES];
HKSampleQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:sampleType predicate:predicate limit:HKObjectQueryNoLimit sortDescriptors:@[sortDescriptor] resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
if (!error && results) {
for (HKQuantitySample *samples in results) {
// your code here
}
}
}];
// Execute the query
[healthStore executeQuery:sampleQuery];