2017-08-30 80 views
1

我一直在嘗試一段時間獲取心率,以便在圖表中繪製。正如文檔中提到的,心率可以通過HKStatisticsCollectionQuery獲取。我正在嘗試從當前日期獲取一週的數據。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.Am我做錯了什麼在這裏?

回答

3

問題不在你認爲的地方,結果用統計查詢返回,但是在心率的情況下,它並沒有給出心跳數量,所以HKQuantity *quantity = result.sumQuantity;返回nil。如果您要正確檢查,您會看到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; 
} 
+0

感謝您的迴應。我會嘗試一下代碼並讓你知道。 –

+0

self.lastAnchor未定義 – coolcool1994

+0

您可以使用開始日期作爲錨點,它應該可以工作。 –