HealthKit无法读取步骤数据 [英] HealthKit cannot read steps data

查看:184
本文介绍了HealthKit无法读取步骤数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HealthKit从我的iOS设备读取步骤数据.

I'm working with HealthKit to read steps data from my iOS device.

这是我的代码:

if ([HKHealthStore isHealthDataAvailable]) {
        __block double stepsCount = 0.0;
        self.healthStore = [[HKHealthStore alloc] init];
        NSSet *stepsType =[NSSet setWithObject:[HKObjectType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount]];
        [self.healthStore requestAuthorizationToShareTypes:nil readTypes:stepsType completion:^(BOOL success, NSError * _Nullable error) {
            if (success) {
                HKSampleType *sampleType = [HKSampleType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
                HKSampleQuery *sampleQuery = [[HKSampleQuery alloc] initWithSampleType:sampleType predicate:nil limit:HKObjectQueryNoLimit sortDescriptors:nil resultsHandler:^(HKSampleQuery *query, NSArray *results, NSError *error) {
                    if (error != nil) {
                        NSLog(@"results: %lu", (unsigned long)[results count]);
                        for (HKQuantitySample *result in results) {
                            stepsCount += [result.quantity doubleValueForUnit:[HKUnit countUnit]];
                        }
                        NSLog(@"Steps Count: %f", stepsCount);
                    } else {
                        NSLog(@"error:%@", error);
                }];
                [self.healthStore executeQuery:sampleQuery];
                [self.healthStore stopQuery:sampleQuery];

                NSLog(@"steps:%f",stepsCount);
            }
        }];
    }

我在具有步骤数据的iPhone6上构建并运行代码,并且在设置"->隐私"->健康"中,确实允许该应用读取数据,但日志区域仅显示:

I build and run the code on an iPhone6 which does have steps data and in the Settings -> Privacy -> Health, the app does have been allowed to read data, but the log area only shows:

steps:0.000000

我在for循环和NSLog(@"error:%@", error)上放置了一个断点,但是应用程序没有中断.

I put a break point on the for-loop and on the NSLog(@"error:%@", error), but the app does not break.

有人可以帮忙吗?

推荐答案

您的代码在有机会运行之前立即停止查询.对于此查询,根本没有理由调用stopQuery:,除非您想在查询结束前取消它.由于查询的寿命不长(没有updateHandler),因此在调用resultsHandler之后,查询将立即停止.

Your code stops the query immediately, before it has a chance to run. For this query, there is no reason to call stopQuery: at all unless you want to cancel a query before it finishes. Since the query is not long lived (it doesn't have an updateHandler), it will stop immediately after the resultsHandler is called.

第二个问题是您的代码尝试过早记录步数.查询异步运行,查询完成后将在后台线程上调用resultsHandler.我建议在该块内记录stepsCount.

The second problem is that your code attempts to log step count too soon. The query runs asynchronously, and the resultsHandler will be called on a background thread once the query completes. I'd suggest logging stepsCount inside the block.

最后,如果要计算用户的步数,则应使用HKStatisticsQuery,而不是对HKSampleQuery的结果求和. HKStatisticsQuery效率更高,当HealthKit中有多个重叠数据源时,将产生正确的结果.例如,如果他们同时拥有iPhone和Apple Watch,则当前的实现将重复计算用户的步骤.

Finally, if you want to count the user's steps you should us an HKStatisticsQuery instead of summing the results of an HKSampleQuery. HKStatisticsQuery is more efficient and will yield correct results when there are multiple sources of overlapping data in HealthKit. Your current implementation will double count the user's steps if they have both an iPhone and an Apple Watch, for instance.

这篇关于HealthKit无法读取步骤数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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