【发布时间】:2016-10-28 07:27:28
【问题描述】:
我的应用使用 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];
【问题讨论】:
标签: ios objective-c healthkit hkhealthstore hksamplequery