从 HealthKit 监控心率 --> HKAnchoredObjectQuery 仅在 applicationDidBecomeActive 之后调用(BUG 或 FEATURE?)

2024-07-01

我正在编写一个简单的应用程序,每当将新的健康率值写入 HealthKit 时,就可以从 HealthKit 监控心率(HKQuantityTypeIdentifierHeartRate)。

正如在 WWDC2015(第 203 场会议)上看到的,我正在使用HK锚定对象查询它应该适用于添加和删除对象。每当我启动应用程序时,我都会调用HKQuery对于最新的对象和执行查询效果很好!但即使样本存在,我也没有得到新的样本,但如果我将应用程序置于后台并再次置于前台,我将获得所有新的心率。这是一个错误吗?或者我该如何在不将应用程序置于后台和前台的情况下监测心率?

这是我正在使用的代码(所有内容都存储在 AppDelegate 中),我正在调用[self requestAccessDataTypes]; from didFinishLaunchingWithOptions:

[healthStore enableBackgroundDeliveryForType:sampleType frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error) {}];

HKQuery *query = [self createHeartRateStreamingQuery:datum];
    if (query) {
        [healthStore executeQuery:query];
    }
    else
    {
        NSLog(@"workout can not start");
    }

-(HKQuery*)createHeartRateStreamingQuery:(NSDate*)workoutStartDate
{
    NSLog(@"%@ - createHeartRateStreamingQuery", [self class]);

    if ([HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate]) {
        HKQuantityType *quantityType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];

        HKAnchoredObjectQuery * heartRateQuery = [[HKAnchoredObjectQuery alloc] initWithType:quantityType predicate:nil anchor:anchor limit:HKObjectQueryNoLimit resultsHandler:^(HKAnchoredObjectQuery * _Nonnull query, NSArray<__kindof HKSample *> * _Nullable sampleObjects, NSArray<HKDeletedObject *> * _Nullable deletedObjects, HKQueryAnchor * _Nullable newAnchor, NSError * _Nullable error) {

            if (!error) {
                anchor = newAnchor;
                [self updateHeartRate:sampleObjects];

            }

        }];
        heartRateQuery.updateHandler = ^void(HKAnchoredObjectQuery *query, NSArray<__kindof HKSample *> * __nullable addedObjects, NSArray<HKDeletedObject *> * __nullable deletedObjects, HKQueryAnchor * __nullable newAnchor, NSError * __nullable error)
        {
            if (!error) {
                anchor = newAnchor;
                [self updateHeartRate:addedObjects];

            }

        };
        return heartRateQuery;
    }
    return nil;
}

您缺少观察 HealthKit 变化的关键部分。它被称为HKObserverQuery.

Docs https://developer.apple.com/library/ios/documentation/HealthKit/Reference/HKObserverQuery_Class/index.html#//apple_ref/doc/uid/TP40014747

观察者查询在后台队列上设置长时间运行的任务。 此任务监视 HealthKit 存储,并在任何时候提醒您 匹配的数据被保存到存储或从存储中删除。观察者查询 让您的应用程序响应其他应用程序和设备所做的更改。

Recap:

你必须把你的HKAnchoredObjectQuery in HKObserverQuery启用后台传送,以便收到有关更新的通知。然后,只要发生这种情况,您就可以执行查询。

Note 1: HKObserverQuery的更新处理程序不会向您提供任何 Apple Health 数据样本。你仍然需要执行你的HKAnchoredObjectQuery使用适当的锚来获取样本。

注意2:你必须设置HKObserverQuery每次您的应用程序启动时。

欲了解更多信息,请查看我的回答here https://stackoverflow.com/questions/26375767/healthkit-background-delivery-when-app-is-not-running/35073904#35073904.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 HealthKit 监控心率 --> HKAnchoredObjectQuery 仅在 applicationDidBecomeActive 之后调用(BUG 或 FEATURE?) 的相关文章

随机推荐