在iOS上高效解析HealthKit的HKSampleQuery结果
我的应用程序使用HealthKit框架检索用户健康数据。我想从HealthKit获得大约25个不同的数据点 为此,我目前在示例查询的完成处理程序中的在iOS上高效解析HealthKit的HKSampleQuery结果,ios,objective-c,healthkit,hkhealthstore,hksamplequery,Ios,Objective C,Healthkit,Hkhealthstore,Hksamplequery,我的应用程序使用HealthKit框架检索用户健康数据。我想从HealthKit获得大约25个不同的数据点 为此,我目前在示例查询的完成处理程序中的for循环中有25个调用是否有任何方法可以组合结果,或更有效地执行此过程? 据我所知,我必须这样做(见下面的代码)。先谢谢你 NSDate *startDate, *endDate; // Use the sample type for step count HKSampleType *sampleType = [HKSampleType quan
for循环中有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];
您应该并行执行查询。
这使HealthKit能够高效地执行查询。
如果您这样做,HealthKit会为您进行优化。
实现这一点的最优雅和可读的方法可能是循环。
但写25行也是如此
您不需要做任何事情就可以将查询放入后台队列。HealthKit为你做这件事
有时你会收到25次回电