Ios HKObserver更新Handler停止执行

Ios HKObserver更新Handler停止执行,ios,background-process,healthkit,hkobserverquery,Ios,Background Process,Healthkit,Hkobserverquery,我需要上传服务器上的步骤,即使应用程序是在后台。我已经为HKQuantityTypeIdentifierStepCount添加了带有enableBackgroundDeliveryForType的HKObserverQuery 当从观察员处收到更新通知时,它将查询一天的总步骤(HKStatisticsCollectionQuery) 到目前为止,它工作正常,在执行查询过程之后,它停止了,结果没有收到。步骤在apple health中,但结果没有返回 有人知道它为什么停止或不返回结果吗 [heal

我需要上传服务器上的步骤,即使应用程序是在后台。我已经为HKQuantityTypeIdentifierStepCount添加了带有enableBackgroundDeliveryForType的HKObserverQuery

当从观察员处收到更新通知时,它将查询一天的总步骤(HKStatisticsCollectionQuery)

到目前为止,它工作正常,在执行查询过程之后,它停止了,结果没有收到。步骤在apple health中,但结果没有返回

有人知道它为什么停止或不返回结果吗

[healthStore enableBackgroundDeliveryForType: [HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount] frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error) {
    NSLog(@"Delegate Observation registered error for steps=%@",error);
}];
HKobserverQuery和enableBackgroundDeliveryForType都是从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];
我已经创建了一个自定义类,可以从apple health获取步骤并上传到服务器,下面是获取步骤的方法代码

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];
如果提供的信息不够,请询问任何问题。我不知道我做错了什么