运行状况与HealthKit的处理方式不同,Swift的处理方式不同 [英] Health handles multiple step sources differently than HealthKit—swift

查看:124
本文介绍了运行状况与HealthKit的处理方式不同,Swift的处理方式不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Swift iOS应用程序与HealthKit相连,以向用户显示他们到目前为止已经执行了多少步骤.在大多数情况下,这是成功的.当步骤的唯一来源是通过iPhone的内置计步器功能记录的步骤时,一切正常,并且我的应用程序显示的步骤计数与Health应用程序的步骤计数匹配.但是,当有多个数据源时(在我的个人iPhone上,我的Pebble Time智能手表和iPhone的计步器都向Health提供了步骤),我的应用程序异常运行,记录了两者的所有步骤. iOS Health应用程序会根除重复的步骤(之所以可以这样做,是因为我的iPhone和Pebble报告每60秒都会执行一次Health操作),并显示准确的每日步骤计数,而我的应用程序从HealthKit获取的数据包括这两个步骤的所有步骤来源,造成很大的误差.

My Swift iOS app connects with HealthKit to show the user how many steps they have taken so far in the day. For the most part, this is successful. When the sole source of steps is steps recorded by the iPhone's built-in pedometer function, everything works fine and the step count shown by my app matches with the Health app's step count. However, when there are multiple sources of data—on my personal iPhone, my Pebble Time smartwatch and the iPhone's pedometer both feed steps to Health—my app freaks out, recording all the steps from both. Whereas the iOS Health app roots out duplicate steps (which it can do because both my iPhone and my Pebble report steps to Health every 60 seconds) and shows an accurate daily step count, the data my app gets from HealthKit includes all the steps from both sources, causing great inaccuracies.

我该如何利用Health应用程序的最终结果(步数准确),而不是利用HealthKit夸大的​​步骤数据流?

How can I tap into the Health app's final result, where the step count is accurate, instead of tapping into HealthKit's stream of over-inflated step data?

更新:这是我用来获取每日健康数据的代码:

UPDATE: Here's the code I use to get daily health data:

func recentSteps2(completion: (Double, NSError?) -> () )
    {

        checkAuthorization() // checkAuthorization just makes sure user is allowing us to access their health data.
        let type = HKSampleType.quantityTypeForIdentifier(HKQuantityTypeIdentifierStepCount) // The type of data we are requesting


        let date = NSDate()
        let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
        let newDate = cal.startOfDayForDate(date)
        let predicate = HKQuery.predicateForSamplesWithStartDate(newDate, endDate: NSDate(), options: .None) // Our search predicate which will fetch all steps taken today

        // The actual HealthKit Query which will fetch all of the steps and add them up for us.
        let query = HKSampleQuery(sampleType: type!, predicate: predicate, limit: 0, sortDescriptors: nil) { query, results, error in
            var steps: Double = 0

            if results?.count > 0
            {
                for result in results as! [HKQuantitySample]
                {
                    steps += result.quantity.doubleValueForUnit(HKUnit.countUnit())
                }
            }

            completion(steps, error)
        }

        storage.executeQuery(query)
    }

推荐答案

您的代码是重复计算步骤,因为它只是对HKSampleQuery的结果求和.样本查询将返回与给定谓词匹配的所有样本,包括来自多个源的重叠样本.如果要使用HKSampleQuery准确计算用户的步数,则必须检测重叠的样本并避免对它们进行计数,这将很繁琐且难以正确执行.

Your code is over-counting steps because it simply sums the results of an HKSampleQuery. A sample query will return all samples matching the given predicate, including overlapping samples from multiple sources. If you wanted to accurately compute the user's step count using HKSampleQuery, you'd have to detect overlapping samples and avoid counting them, which would be tedious and difficult to do correctly.

Health使用HKStatisticsQueryHKStatisticsCollectionQuery来计算合计值.这些查询可以为您计算总和(以及其他合计值),并且可以高效地进行计算.不过,最重要的是,它们对重复的样本进行重复数据删除,以避免计数过多.

Health uses HKStatisticsQuery and HKStatisticsCollectionQuery to compute aggregate values. These queries calculate the sum (and other aggregate values) for you, and do so efficiently. Most importantly, though, they de-duplicate overlapping samples to avoid over-counting.

HKStatisticsQuery的文档包括示例代码.

The documentation for HKStatisticsQuery includes sample code.

这篇关于运行状况与HealthKit的处理方式不同,Swift的处理方式不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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