繁体   English   中英

HKStatisticsCollectionQuery 用于获取心率健康套件

[英]HKStatisticsCollectionQuery for fetching Heart Rate Health Kit

我一直在尝试获取心率以绘制图表。 正如文档中提到的,心率可以通过 HKStatisticsCollectionQuery 获取。 我正在尝试从当前日期获取一周的数据。

但我无法获取获取的数据。 下面是我使用 HKStatisticsCollectionQuery 访问心率的代码:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *interval = [[NSDateComponents alloc] init];
NSDate *anchorDate = [[NSDate alloc] init];
NSDateComponents *anchorComponents =
    [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth |
     NSCalendarUnitYear | NSCalendarUnitWeekday fromDate:[NSDate date]];

NSDate *currentDisplayEndDate = [NSDate date];
NSDate *newDate = [calendar startOfDayForDate: currentDisplayEndDate];  NSDate *startDate = [newDate dateByAddingTimeInterval:-6*24*60*60];
anchorDate = startDate;
 NSPredicate *predicate = [HKQuery predicateForSamplesWithStartDate:self.startDate endDate:_currentDisplayEndDate options:HKQueryOptionStrictStartDate];

HKQuantityType *quantityType =
    [HKObjectType quantityTypeForIdentifier:quantityId];

    // Create the query

    HKStatisticsCollectionQuery *query =
    [[HKStatisticsCollectionQuery alloc]
     initWithQuantityType:quantityType
     quantitySamplePredicate:predicate
     options:HKStatisticsOptionDiscreteMax
     anchorDate:anchorDate
     intervalComponents: interval];

    // Set the results handler
    query.initialResultsHandler =
    ^(HKStatisticsCollectionQuery *query, HKStatisticsCollection *results, NSError *error) {

        if (error) {
            // Perform proper error handling here
            NSLog(@"*** An error occurred while calculating the statistics: %@ ***",
                  error.localizedDescription);
        }
        [results
 enumerateStatisticsFromDate:startDate
 toDate:endDate
 withBlock:^(HKStatistics *result, BOOL *stop) {

     HKQuantity *quantity = result.sumQuantity;
     if (quantity) {
         NSDate *date = result.startDate;
         double value = [quantity doubleValueForUnit:[[HKUnit unitFromString:@"count/min"]];

         // Call a custom method to plot each data point.
     }

 }];
    };

    [healthStore executeQuery:query];

我的HKStatistics *results返回 nil。我在这里做错了什么吗?

问题不是你想的那样,结果是通过统计查询返回的,但是在心率的情况下,它并没有给出心跳数量,所以HKQuantity *quantity = result.sumQuantity; 返回零。

如果您检查正确,您会看到results.statistics会为您提供一些有关记录的心率的数据,但没有心率数量,而只有记录数据的开始和结束日期。

我建议,继续和你HKAnchoredQuery一样,我会在这里提供代码:

-(double)get_heartRates
    {
    //code to heart beats average, modify as needed
    NSDate *startDate1 = [NSDate distantPast];
    NSPredicate *Predicate = [HKQuery predicateForSamplesWithStartDate:startDate1 endDate:[NSDate date] options:HKQueryOptionStrictEndDate];
    HKSampleType *object = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];

    sum_Of_HeartRates=0.0;

    HKAnchoredObjectQuery  *heartQuery = [[HKAnchoredObjectQuery alloc] initWithType:object predicate:Predicate anchor:self.lastAnchor limit:0 resultsHandler:^(HKAnchoredObjectQuery *query, NSArray<HKSample *> *sampleObjects, NSArray<HKDeletedObject *> *deletedObjects, HKQueryAnchor *newAnchor, NSError *error) {

        NSLog(@"Sample counts:%ld",sampleObjects.count);
        for(int i=0;i<(int)sampleObjects.count;i++)
        {
            HKQuantitySample *sample = (HKQuantitySample *)[sampleObjects objectAtIndex:i];
            HKQuantity *quantity = sample.quantity;
            double bpm_Values= [quantity doubleValueForUnit:[HKUnit unitFromString:@"count/min"]];
            sum_Of_HeartRates=sum_Of_HeartRates+bpm_Values;

        }
        avg_heartBeats=sum_Of_HeartRates/(int)sampleObjects.count;
    }];
    [heartQuery setUpdateHandler:^(HKAnchoredObjectQuery *query, NSArray<HKSample *> *SampleArray, NSArray<HKDeletedObject *> *deletedObjects, HKQueryAnchor *Anchor, NSError *error) {

        HKQuantitySample *sample = (HKQuantitySample *)[SampleArray objectAtIndex:0];
        HKQuantity *quantity = sample.quantity;
        new_Updated_Data =[quantity doubleValueForUnit:[HKUnit unitFromString:@"count/min"]];
        NSLog(@"new quantity:%f",new_Updated_Data);
    }];
    [self.healthStore executeQuery:heartQuery];
    NSLog(@"updated data %f",new_Updated_Data);
    return avg_heartBeats;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM