gpt4 book ai didi

ios - HKStatisticsCollectionQuery获取心率健康套件

转载 作者:行者123 更新时间:2023-12-01 18:05:38 33 4
gpt4 key购买 nike

我一直在尝试获取心率,以便在图表中进行绘制。如文档中所述,可以通过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;返回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;
}

关于ios - HKStatisticsCollectionQuery获取心率健康套件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45957689/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com