我有一些从 healthkit 查询数据的代码。我们从 healthkit 以及最近的锻炼中获得了各种样本和数量。在我的手机上(通常是每天一次锻炼——尽管 Basis 将内容存储为多次锻炼),锻炼查询大约需要 8 秒,但所有其他类型的完成时间不到一秒。
感觉就像,在幕后,这个查询正在做一个线性扫描,其他的都被索引了。想知道是否有其他人遇到过这种情况或有任何想法?
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];