Error Domain=com.apple.healthkit Code=3 "Unable to invalidate interval: no data source available."

I have used below source code to get the steps from HealthKit. It's working fine many users but few users face the issue regards to get the steps from HealthKit in the our Application. After tracking the logs that issue found **: Error Domain=com.apple.healthkit Code=3 "Unable to invalidate interval: no data source available." **

Please help to know what exactly issue in the below source code.

-(void) HealthKitSteps {

    HKHealthStore *healthStore = [[HKHealthStore alloc] init];

    NSCalendar *calendar = [NSCalendar currentCalendar];

    [calendar setTimeZone:[NSTimeZone localTimeZone]];

    NSDateComponents *interval = [[NSDateComponents alloc] init];

    interval.hour = 2;

  NSDateComponents *anchorComponents = [calendar components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];

    anchorComponents.hour = 0;

    NSDate *anchorDate = [calendar dateFromComponents:anchorComponents];

    HKQuantityType *quantityType = [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];

//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"metadata.%K != YES", HKMetadataKeyWasUserEntered];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    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) {

            // Perform proper error handling here

            [UIUtils NSLogFunction:[NSString stringWithFormat:@"*** An error occurred while calculating the statistics: %@ ***",error.localizedDescription] dict:nil];

        }

        else

        {

            NSDate* sourceDate = [NSDate date];

            NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:0 sinceDate:sourceDate];

            NSDateFormatter *endDateFormatter = [[NSDateFormatter alloc] init];

            [endDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

            [endDateFormatter setTimeZone:[NSTimeZone localTimeZone]];

            

            NSString* localTime = [endDateFormatter stringFromDate:destinationDate];

            NSDate* endDate = [endDateFormatter dateFromString:localTime];

            

            NSDateFormatter *objDateFormatter = [[NSDateFormatter alloc] init];

            [objDateFormatter setDateFormat:@"yyyy-MM-dd"];

            [objDateFormatter setTimeZone:[NSTimeZone localTimeZone]];

            

            NSDate *startDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:-self->diffDaysFromLastSync toDate:endDate options:0];

            

            NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian];

            [calendar setTimeZone:[NSTimeZone localTimeZone]];

            NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:startDate];

            [components setTimeZone:[NSTimeZone localTimeZone]];

            [components setHour:0];

            startDate = [calendar dateFromComponents:components];

            

            NSLog(@"Healthkit : startDate : %@ endDate : %@",[endDateFormatter stringFromDate:startDate],[endDateFormatter stringFromDate:endDate]);

            

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

            HKQuantity *quantity = result.sumQuantity;

                if (quantity) {

                    double value = [quantity doubleValueForUnit:[HKUnit countUnit]];

                    NSString *count = [NSString stringWithFormat:@"%.f",value];

                    NSString *dateOfCount  = [objDateFormatter stringFromDate:[result startDate]];

                    NSString* dateTime = [endDateFormatter stringFromDate:[result startDate]];

                    NSLog(@"results quantity start Date %@ sum %@ Count %@",result.startDate, result.sumQuantity,count);

                }

                else

                {

                    NSLog(@"results not found");

                }

            }];

        }

    };

    [healthStore executeQuery:query];

    });

}

Post not yet marked as solved Up vote post of RahulS Down vote post of RahulS
692 views