从HealthKit获得听到率

时间:2019-01-06 16:08:40

标签: ios health-kit

我有一个相当简单的代码,可以使您心跳加速,并且不返回任何内容。在文档中没有错误,没有警告,没有任何内容,也没有什么可以帮助我的。我缺少明显的东西吗?

这是代码:

- (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直接运行到手机上。很明显,我的手机上听到了速率,这是我做错了什么吗?

谢谢

1 个答案:

答案 0 :(得分:0)

对我感到羞耻!

开始日期和结束日期已切换...

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