从HealthKit监视心率-> HKAnchoredObjectQuery仅在applicationDidBecomeActive之后调用(BUG或FEATURE?) [英] Monitor heart rate from HealthKit --> HKAnchoredObjectQuery only called after applicationDidBecomeActive (BUG or FEATURE?)

查看:175
本文介绍了从HealthKit监视心率-> HKAnchoredObjectQuery仅在applicationDidBecomeActive之后调用(BUG或FEATURE?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am writing a simple app to monitor the heart rate (HKQuantityTypeIdentifierHeartRate) from HealthKit whenever a new health rate value is written to HealthKit.

正如在WWDC2015(会议203)上所看到的,我正在使用 HKAnchoredObjectQuery ,它应该可以用于添加和删除对象.每当我启动应用程序时,我都会调用 HKQuery 来获取最新的对象,并调用 executingQuery 可以正常工作!但是即使有样本,我也没有得到任何新样本,但是如果我将应用程序置于后台,然后又再次置于前台,那么我将获得所有新的心率.这是一个错误吗?或者我应该怎么做才能在不使应用程序前后移动的情况下监视心率?

As seen at WWDC2015 (session 203) I am using a HKAnchoredObjectQuery which should work for adding and deleting objects. Whenever I start the app I am calling the HKQuery for the newest objects and executingQuery which works fine!!! But I am getting no new samples even if the samples are there, but if I bring the app to the background and again to the foreground I am getting all the new heart rates. IS IT A BUG? Or what shall I do to monitor the heart rate without bringing the app to the back- and foreground?

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

Here is the code I am using (everything is stored in the AppDelegate), I am calling [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.

You are missing a crucial piece for observing changes in HealthKit. It's called HKObserverQuery.

文档

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

Observer queries set up a long-running task on a background queue. This task watches the HealthKit store, and alerts you whenever matching data is saved to or removed from the store. Observer queries let your app respond to changes made by other apps and devices.

回顾:

您必须将HKAnchoredObjectQuery包装在HKObserverQuery中,并启用后台传送,以便获得有关更新的通知.然后,您可以随时执行查询.

You have to wrap your HKAnchoredObjectQuery in HKObserverQuery with background delivery enabled in order to get notified about updates. You can then execute your query whenever that happens.

注1:HKObserverQuery的更新处理程序不会提供任何Apple Health数据示例.您仍然必须使用适当的锚点执行HKAnchoredObjectQuery来获取示例.

Note 1: HKObserverQuery's update handler will NOT give you any Apple Health data samples. You still have to execute your HKAnchoredObjectQuery with a proper anchor to get the samples.

注意2:每次启动应用程序时,您都必须设置HKObserverQuery.

Note 2: You have to set up HKObserverQuery every time your app launches.

有关更多信息,请在这里查看答案.

这篇关于从HealthKit监视心率-> HKAnchoredObjectQuery仅在applicationDidBecomeActive之后调用(BUG或FEATURE?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆