首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HKSampleQuery不检索最新步骤

HKSampleQuery不检索最新步骤
EN

Stack Overflow用户
提问于 2017-07-24 19:56:31
回答 1查看 296关注 0票数 2

我使用以下查询来使用healthkit检索特定一天的步骤

代码语言:javascript
复制
//Predicate
NSDate *startDate = [[NSCalendar currentCalendar] startOfDayForDate:[NSDate date]];
NSDateComponents *comp = [NSDateComponents new];
comp.day = 1;
comp.second = -1;
NSDate *endDate = [[NSCalendar currentCalendar] dateByAddingComponents:comp toDate:startDate options: 0];
NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:startDate endDate:[[NSDate date] dateByAddingDays:endDate] options: HKQueryOptionStrictEndDate];


NSSortDescriptor *timeSortDescriptor = [[NSSortDescriptor alloc] initWithKey:HKSampleSortIdentifierStartDate ascending:YES];

HKQuantityType *type = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];


HKSampleQuery *stepsQuery = [[HKSampleQuery alloc] initWithSampleType: type
                                                                predicate: predicate limit:HKObjectQueryNoLimit
                                                          sortDescriptors:@[timeSortDescriptor]
                                                           resultsHandler: ^(HKSampleQuery *query, NSArray *resultsSteps, NSError *error) {
    }....

问题:查询不返回最新步骤。有两种方法我可以得到最新的步骤

  1. 在打开我的应用程序之前打开健康应用程序-这将更新健康应用程序中的最新步骤&上面的查询返回最新的步骤。
  2. 在20-30分钟后打开应用程序,它显示了最新的步骤。

我是不是遗漏了什么?

它是一个遗留代码库,所以没有Swift :)

EN

回答 1

Stack Overflow用户

发布于 2018-01-15 14:35:58

在示例查询下面添加一个观察者查询修复了这个问题,我仍然找不到这个问题的根源

代码语言:javascript
复制
 HKObserverQuery *ibsQuery  = [[HKObserverQuery alloc] initWithSampleType:steps predicate:pred updateHandler:^(HKObserverQuery * _Nonnull query, HKObserverQueryCompletionHandler  _Nonnull completionHandler, NSError * _Nullable error) {
        //Required completion block
    }];
 [healthStore executeQuery:ibsQuery];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45289009

复制
相关文章

相似问题

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