我写了一个代码来获取Health kit中的心率值。代码工作正常,但在Health kit中更新了新的心脏值。我必须进入主屏幕然后多任务我的应用程序以获取更新的结果。我的目标是在我的应用程序上获得更新结果而无需重新打开或多任务处理,请帮助我,因为我是iOS开发的新手。 我的代码: -
-(void)get_heartRates
{
//code to get the updated heart beats
HKSampleType *sampleType =
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
HKObserverQuery *query =
[[HKObserverQuery alloc]
initWithSampleType:sampleType
predicate:nil
updateHandler:^(HKObserverQuery *query,
HKObserverQueryCompletionHandler completionHandler,
NSError *error) {
if (error) {
NSLog(@"error occured while setting observer. %@ ",
error.localizedDescription);
abort();
}
// Take whatever steps are necessary to update your app's data and UI
// This may involve executing other queries
[self executeAnchoredQuery];
// If you have subscribed for background updates you must call the completion handler here.
// completionHandler();
}];
[self.healthStore executeQuery:query];
}
-(void)executeAnchoredQuery
{
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);
NSLog(@"%f", avg_heartBeats);
}
答案 0 :(得分:0)
查看以下代码是否可以帮助您......
HKSampleType *sampleType =
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
HKObserverQuery *query =
[[HKObserverQuery alloc]
initWithSampleType:sampleType
predicate:nil
updateHandler:^(HKObserverQuery *query,
HKObserverQueryCompletionHandler completionHandler,
NSError *error) {
if (error) {
NSLog(@"error occured while setting observer. %@ ",
error.localizedDescription);
abort();
}
// Take whatever steps are necessary to update your app's data and UI
// This may involve executing other queries
[self executeAnchoredQuery];
// If you have subscribed for background updates you must call the completion handler here.
// completionHandler();
}];
[self.healthStore executeQuery:query];
然后你编写anchoredQuery代码的函数,这可能会让你知道代码流:
-(void)executeAnchoredQuery
{
HKSampleType *sampleType =
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
HKAnchoredObjectQuery *query =
[[HKAnchoredObjectQuery alloc]
initWithType:sampleType
predicate:nil
anchor:self.anchor
limit:HKObjectQueryNoLimit
resultsHandler:^(HKAnchoredObjectQuery * _Nonnull query,
NSArray<__kindof HKSample *> * _Nullable sampleObjects,
NSArray<HKDeletedObject *> * _Nullable deletedObjects,
HKQueryAnchor * _Nullable newAnchor,
NSError * _Nullable error) {
if (error != nil) {
// Add proper error handling here...
NSLog(@"*** Unable to query for step counts: %@ ***",
error.localizedDescription);
abort();
}
// Process the results...
self.anchor = newAnchor;
for (HKQuantitySample *sample in sampleObjects) {
[self addStepCountSample:sample];
}
for (HKDeletedObject *sample in deletedObjects) {
[self removeStepCountSample:sample];
}
NSLog(@"Done!");
}];
[self.healthStore executeQuery:query];
}
请查看Apple开发人员文档以获取更多详细信息。