HKObserver updateHandler stop execution

194 Views Asked by At

I need to upload steps on server even when app is in background. I have added HKObserverQuery with enableBackgroundDeliveryForType for HKQuantityTypeIdentifierStepCount.

When update notification receive from observer, it will query for one day total steps (HKStatisticsCollectionQuery).

Up to this it works fine, after execute query process it stoped, result not received.Steps are there in apple health but result not return.

Can anyone know why it stoped or not returning result?

[healthStore enableBackgroundDeliveryForType: [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount] frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error) {
    NSLog(@"Delegate Observation registered error for steps=%@",error);
}];

HKobserverQuery and enableBackgroundDeliveryForType both added from didFinishLaunchingWithOptions.

HKSampleType *quantityType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];

HKObserverQuery *query =
[[HKObserverQuery alloc]
 initWithSampleType:quantityType
 predicate:nil
 updateHandler:^(HKObserverQuery *query,
                 HKObserverQueryCompletionHandler completionHandler,
                 NSError *error) {

      CustomClass *customVC = [[CustomClass alloc] init];
             [customVC fetchSteps:completionHandler];

 }];
[self.healthApp.healthStore executeQuery:query];

I have created custom class which fetch steps from apple health and upload to server, below is method code which fetch steps.

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *interval = [[NSDateComponents alloc] init];
interval.day = 1;

// Set the anchor date to Monday at 3:00 a.m.
NSDateComponents *anchorComponents =
[calendar components:NSCalendarUnitDay | NSCalendarUnitMonth |
 NSCalendarUnitYear fromDate:[NSDate date]];

anchorComponents.day -= 1;
anchorComponents.hour = 0;

NSDate *anchorDate = [calendar dateFromComponents:anchorComponents];

NSLog(@"Anchor date::::: %@",anchorDate);

HKQuantityType *quantityType =
[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];


// Create the query
HKStatisticsCollectionQuery *query =
[[HKStatisticsCollectionQuery alloc]
 initWithQuantityType:quantityType
 quantitySamplePredicate:nil
 options:HKStatisticsOptionCumulativeSum
 anchorDate:anchorDate
 intervalComponents:interval];

// Set the results handler
query.initialResultsHandler =
^(HKStatisticsCollectionQuery *query, HKStatisticsCollection *results, NSError *error) {

    if (error) {

        NSLog(@"An error occurred while calculating the statistics:::: %@",error);
        return ;

    }

    NSCalendar *cal = [NSCalendar currentCalendar];
    NSDateComponents *components = [[NSDateComponents alloc] init];
    NSDateFormatter *formate=[[NSDateFormatter alloc] init];
    [formate setTimeZone:[NSTimeZone localTimeZone]];
    [formate setDateFormat:@"yyyy-MM-dd"];

    NSDate *startDate;
    NSDate *endDate;

    NSLog(@"Current date::: %@",startdt);
    NSString *dateStr = [formate stringFromDate:startdt];
    startDate = [formate dateFromString:dateStr];

    dateStr = [NSString stringWithFormat:@"%@ 23:59:59",dateStr];
    [formate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    endDate = [formate dateFromString:dateStr];

    [components setDay:-1];
    startDate = [cal dateByAddingComponents:components toDate:startDate options:0];


    NSDateFormatter *form=[[NSDateFormatter alloc] init];
    [form setDateFormat:@"dd-MM-yyyy HH:mm:ss"];


    // Gether all data
    [results
     enumerateStatisticsFromDate:startDate
     toDate:endDate
     withBlock:^(HKStatistics *result, BOOL *stop) {

         HKQuantity *quantity = result.sumQuantity;
         if (quantity) {

             double value = [quantity doubleValueForUnit:hkUnit];

             NSString *strValue = [NSString stringWithFormat:@"%f",value];
             NSDate *date = result.startDate;

             [self uploadSteps:date steps:strValue];

         }

     }];


};

[self.healthStore executeQuery:query];

If given information is not enough please ask any. I don't know what i have did wrong.

0

There are 0 best solutions below