HKAnchoredQuery Update Handler funktioniert nicht richtig

Ich habe einen Code geschrieben, um die Herzfrequenzwerte aus dem Health Kit zu erhalten. Der Code funktioniert einwandfrei, aber wenn die neuen Herzwerte im Health Kit aktualisiert werden. Ich muss zum Hauptbildschirm kommen und dann meine App multitasking, um die aktualisierten Ergebnisse zu erhalten. Mein Ziel ist es, das aktualisierte Ergebnis auf meiner App zu erhalten, ohne sie erneut zu öffnen oder Multitasking auszuführen. Bitte helfen Sie mir, da ich neu in der iOS-Entwicklung bin. Mein Code: -

  -(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);

 }

Antworten auf die Frage(2)

Ihre Antwort auf die Frage