HKObserverQuery is randomly called twice in a row

I have a problem that I am trying to solve, I have setup HKObserveryQuery, which works fine and collects new data for me.

However, the problem is that sometimes, when I return to the Health application and delete the item after I manually added it to the Health application, I noticed that HKObserverQueryI set fires two times very close to each other, which I am trying to solve because I am using this observer to load some data later, and I do not want to duplicate.

I would appreciate any help. The code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self setup];

    return YES;
}

- (void)setup
{
    if ([HKHealthStore isHealthDataAvailable])
    {
        self.healthStore = [[HKHealthStore alloc]init];

        NSSet *readTypes = [NSSet setWithObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate]];

        [self.healthStore requestAuthorizationToShareTypes:nil
                                                 readTypes:readTypes
                                                completion:^(BOOL success, NSError *error)
         {
             if (!error && success)
             {
                 [self observeHR];

                 [self.healthStore enableBackgroundDeliveryForType:
                 [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate]
                 frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error){}];
             }
         }];
    }
}

- (void)observeHR
{
    HKObserverQuery *query = [[HKObserverQuery alloc]initWithSampleType:[HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate]
                        predicate:nil
    updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error)
    {
        if (!error)
        {   
            // Randomly called twice *VERY* close together
            NSLog(@"Query");
            [self queryWithCompletionHandler:completionHandler];
        }
        else
        {
            if (completionHandler)
            {
                completionHandler();
            }
        }
    }];

    [self.healthStore executeQuery:query];
}

Console exit, pay attention to the time: This happens when you remove one item from the Health application, which is incorrect.

2014-12-29 16:50:20.121 TestApp[174:5674] Query
2014-12-29 16:50:20.124 TestApp[174:5674] Query
+4
2

, , BOOL, HKObserverQuery . :

- (void)observeHR
{
    HKObserverQuery *query = [[HKObserverQuery alloc]initWithSampleType:[HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate]
                        predicate:nil
    updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error)
    {
        if (!self.queryInProgress)
        {
            self.queryInProgress = YES;

            if (!error)
            {
                [self queryWithCompletionHandler:completionHandler];
            }
            else
            {
                self.queryInProgress = NO;

                if (completionHandler)
                {
                    completionHandler();
                }
            }
        }
        else
        {
            NSLog(@"Query already in progress");
        }
    }];

    [self.healthStore executeQuery:query];
}
+2

, updateHandler HKObserverQuery, . updateHandler , , , , . , , . , , , , HKAnchoredObjectQuery , .

+4

Source: https://habr.com/ru/post/1569373/


All Articles