I have some code which is querying data from healthkit. We get various samples and quantities from the healthkit as well as recent workouts. On my phone (usually a workout every day - although the Basis stores things as multiple workouts), the workout query takes about 8 seconds, but all the other types finish in less than a second.
It feels like, under the covers, this query is doing a linear scan where the others are indexed or something. Wondered if anybody else had run into this or had any thoughts?
NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:[self dateByCalculatingWithNumberOfDays:-1 date:[NSDate date]]
endDate:[NSDate date]
options:HKQueryOptionStrictStartDate | HKQueryOptionStrictEndDate];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:HKSampleSortIdentifierStartDate ascending:NO];
HKSampleQuery *query = [[HKSampleQuery alloc] initWithSampleType:[HKSampleType workoutType]
predicate:predicate
limit:0
sortDescriptors:@[sortDescriptor]
resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
self.workoutEntries = [[NSMutableArray alloc] initWithCapacity:results.count];
for (HKWorkout *workout in results) {
WorkoutObject *workoutObject = [WorkoutObject workoutObjectWithWorkout:workout];
[self.workoutEntries addObject:workoutObject];
}
[self fetchHeartRate];
}];
[self.healthStore executeQuery:query];