首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在HKSampleQuery上高效解析HealthKit的HealthKit结果

在HKSampleQuery上高效解析HealthKit的HealthKit结果
EN

Stack Overflow用户
提问于 2016-10-28 07:27:28
回答 1查看 752关注 0票数 3

我的应用程序使用HealthKit框架检索用户健康数据。我想从HealthKit获得大约25个不同的数据点。

为此,我目前在示例查询的完成处理程序中有一个for-loop中的25个调用。是否有任何方法组合结果,或更有效地执行此过程?

据我所知,这就是我必须这样做的方法(参见下面的代码)。提前谢谢你。

代码语言:javascript
复制
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];
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-28 21:44:17

您应该并行地执行查询。这使HealthKit能够高效地执行查询。如果您这样做,HealthKit会为您进行优化。最优雅和可读的方法可能是循环。但是写25行也是如此。

不需要做任何事情就可以将查询放到后台队列中。HealthKit为你做这件事。

有些时候,你会得到你的25次回调。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40300146

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档