我的应用程序使用HealthKit框架检索用户健康数据。我想从HealthKit获得大约25个不同的数据点。
为此,我目前在示例查询的完成处理程序中有一个for-loop中的25个调用。是否有任何方法组合结果,或更有效地执行此过程?。
据我所知,这就是我必须这样做的方法(参见下面的代码)。提前谢谢你。
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];发布于 2016-10-28 21:44:17
您应该并行地执行查询。这使HealthKit能够高效地执行查询。如果您这样做,HealthKit会为您进行优化。最优雅和可读的方法可能是循环。但是写25行也是如此。
不需要做任何事情就可以将查询放到后台队列中。HealthKit为你做这件事。
有些时候,你会得到你的25次回调。
https://stackoverflow.com/questions/40300146
复制相似问题