从 HealthKit 获取听力率

Getting hear rate from HealthKit

我有一个相当简单的代码,可以获取心跳,并且不返回任何内容。没有错误,没有警告,什么都没有,文档中也没有任何我能找到的可以帮助我的东西。我是否遗漏了一些明显的东西?

这是代码:

- (void)    readLastDayHeartRateMeasurements
{
    NSCalendar  *calendar   = [NSCalendar currentCalendar] ;
    NSDate *startDate       = [NSDate date] ;
    NSDate *endDate         = [calendar dateByAddingUnit:NSCalendarUnitDay
                                                   value:-1
                                                  toDate:startDate
                                                 options:0] ;

    HKSampleType    *sampleType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate] ;
    NSPredicate     *predicate  = [HKQuery predicateForSamplesWithStartDate:startDate
                                                                    endDate:endDate
                                                                    options:HKQueryOptionNone] ;

    self.query = [[HKSampleQuery alloc] initWithSampleType:sampleType
                                                           predicate:predicate
                                                               limit:HKObjectQueryNoLimit
                                                     sortDescriptors:nil
                                                      resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error)
    {
        NSLog(@"Query %@ got a result with %lu samples, error %@",query,(unsigned long)[results count],error) ;
        [error logDetailsOfError] ;
    }];

    if (self.query)
    {
        NSLog(@"Query %@ about to start",self.query) ;
        [self.hkStore executeQuery:self.query] ;
        NSLog(@"Query %@ started",self.query) ;
    }
    else
    {
        NSLog(@"No query to execute") ;
    }
}

我得到以下日志:

2019-01-06 16:56:06.252736+0100 HeartBeatDetails[1128:193491] Authorization succes : 1
2019-01-06 16:56:06.253984+0100 HeartBeatDetails[1128:193491] Query <HKSampleQuery:0x2832c19a0 inactive> about to start
2019-01-06 16:56:06.254440+0100 HeartBeatDetails[1128:193491] Query <HKSampleQuery:0x2832c19a0 activating> started
2019-01-06 16:56:06.276334+0100 HeartBeatDetails[1128:193492] Query <HKSampleQuery:0x2832c19a0 deactivated> got a result with 0 samples, error (null)

我 运行 从 XCode 直接到我的 phone。因为我的 phone 中显然有听力率,我做错了什么?

谢谢,

真丢脸!

开始日期和结束日期调换...

Correct code :
    NSDate *endDate         = [NSDate date] ;
    NSDate *startDate       = [calendar dateByAddingUnit:NSCalendarUnitHour
                                                   value:-1
                                                  toDate:endDate
                                                 options:0] ;