HealthKit:HKObserverQuery没有被解雇 [英] HealthKit: HKObserverQuery not Firing

查看:146
本文介绍了HealthKit:HKObserverQuery没有被解雇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一个非常基本的事情:设置一个HKObserverQuery,这样我就可以知道各种数据点何时被更改(我确保用户已授权应用程序使用相关数据点。)无论出于何种原因,我都可以在每次启动应用程序时触发查询,但是当我关闭应用程序,进入运行状况应用程序并手动更新数据点时,它不会触发。我已经做了相当多的搜索,但是无法成功使用其他人发布的代码,他们说这些代码对他们有用。

I'm trying to do a pretty basic thing: set up an HKObserverQuery so I can know when various data points are changed (I've made sure that the user has authorized the app to use the data point in question.) For whatever reason, I can get the query to fire every time the app is launched, but it does not fire when I close the app, go into the Health app, and manually update the data point. I've done a fair amount of searching and haven't been able to successfully use the code that others have posted, code that they say works for them.

我'进入Cocoa / Objective C开发两周后,所以我确定我错过了一些明显的东西,但我看不出它是什么。这里的任何指导都会很棒,即使它只是调试方面的建议。由于应用程序本身已关闭,并且我没有收到任何可能在控制台中注销的内容,因此我无法了解正在发生的事情。

I'm two weeks into Cocoa/Objective C development, so I'm sure I'm missing something obvious, but I can't see what it is. Any guidance here would be great, even if it's just advice on debugging. Since the app itself is closed and I'm not getting anything that it might log out in the console, I don't really have any visibility into what's happening.

我用于观察者查询的代码如下:

The code that I'm using for the observer query is below:

   HKQuantityType *heartRate = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierHeartRate];
    [self.healthStore enableBackgroundDeliveryForType:heartRate frequency:HKUpdateFrequencyImmediate withCompletion:^(BOOL success, NSError *error) {
        if (success) {
            NSLog(@"observing heart rate");
            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.bodbot.com/Scripts/wearable_heartrate_changed.php"]];
        }else{
            NSLog(@"FAILED observing heart rate");
        }
    }];
    HKObserverQuery *query = [[HKObserverQuery alloc] initWithSampleType:heartRate predicate:nil updateHandler:^(HKObserverQuery *query, HKObserverQueryCompletionHandler completionHandler, NSError *error) {
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.bodbot.com/Scripts/wearable_heartrate_changed.php"]];
    }];

    [self.healthStore executeQuery:query];

谢谢!

推荐答案

我发现,根据经验(不是来自文档),Observer Query在模拟器中运行时不会触发,但在设备上运行时它会触发。我没有开启后台模式功能。

I have found, empirically (not from documentation), that the Observer Query does NOT fire when running in the simulator but it DOES fire when running on device. And I do not have the background modes capability turned on.

这篇关于HealthKit:HKObserverQuery没有被解雇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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