2017-08-30 22 views
1

私は、グラフでプロットするために心拍数を取得するためにしばらく試しています。 docsで述べたように、心拍数は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 Iとして返されますか?

答えて

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 is undefined – coolcool1994

+0

開始日をアンカーとして使用することができます。 –

関連する問題