我使用以下查询来使用healthkit检索特定一天的步骤
//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) {
}....问题:查询不返回最新步骤。有两种方法我可以得到最新的步骤
我是不是遗漏了什么?
它是一个遗留代码库,所以没有Swift :)
发布于 2018-01-15 14:35:58
在示例查询下面添加一个观察者查询修复了这个问题,我仍然找不到这个问题的根源
HKObserverQuery *ibsQuery = [[HKObserverQuery alloc] initWithSampleType:steps predicate:pred updateHandler:^(HKObserverQuery * _Nonnull query, HKObserverQueryCompletionHandler _Nonnull completionHandler, NSError * _Nullable error) {
//Required completion block
}];
[healthStore executeQuery:ibsQuery];https://stackoverflow.com/questions/45289009
复制相似问题