ios - 从 HealthKit (HKStatistics) 获取最新(最近)的 Step 值

标签 ios swift healthkit

我想获取 Health(Kit) 应用程序条目的最后(又名最近)值。 例如最新的 HKSource 条目中的步长值。

我认为这将与查询构建中的 HKStatisticsOptions.discreteMostRecent 属性一起使用。

当前结果

代码运行良好,没有错误。我的统计信息具有正确的开始和结束日期(如果只有一个值可用,则开始和结束日期相同)。

问题是 statistics.sources 列表总是 nil,就像 statistics.mostRecentQuantity() 返回 nil 一样。

所有与 HKSource 无关的求和操作 sumQuantity() 都可以正常工作。

其他 Stackoverflow 帖子

我会尝试使用限制为 1 和排序描述 from this post 的查询来尝试这个想法,但我也需要在详细 View 中显示其他历史值。这就是为什么我想,我可以只请求数量的日期范围,使用最新的作为我的“最后一个”,所有其他的用于我的历史 TableView 。

代码

func requestMeasurements(completion: @escaping (HKStatistics?, AppError?) -> Void)
    {
        let healthStore = HKHealthStore()

        // Check if HK is available.
        guard HKHealthStore.isHealthDataAvailable() else
        {
            completion(nil, AppError.healthInformationNotAvailable)
            return
        }

        // Check if HK information is available
        guard let quantitiyType = HKQuantityType.quantityType(forIdentifier: .stepCount) else
        {
            completion(nil, AppError.requestedHealthDataTypeNotAvailable)
            return
        }

        let typeSet: Set = [quantitiyType]

        // Request user access to HK and especially this type
        healthStore.requestAuthorization(toShare: nil, read: typeSet)
        { success, error in

            // Ensure that the app has the required access
            if success == false
            {
                print(error?.localizedDescription ?? "<no error description>")
                completion(nil, AppError.noAccessToRequestedInformation)
                return
            }

            // Build query
            let now = Date()
            let lastSync = Calendar.current.startOfDay(for: now)
            let prediction = HKQuery.predicateForSamples(withStart: lastSync, end: now, options: .strictStartDate)

            let query = HKStatisticsQuery(quantityType: quantitiyType, quantitySamplePredicate: prediction, options: HKStatisticsOptions.discreteMostRecent)
            { _, statistics, error in
                // Check for error.
                if let _error = error
                {
                    print("An error occured: \(_error.localizedDescription)")
                    completion(nil, AppError.requestingFailed)
                    return
                }

                // Check if statistics are available.
                guard let _statistics = statistics else
                {
                    completion(nil, AppError.requestingFailed)
                    return
                }

                completion(_statistics, nil)
            }

            // Execure query
            healthStore.execute(query)
        }

最佳答案

如果您只想使用数量等,只需使用 HKSampleQuery 而不是 HKStatisticsQuery

之后,您只需将 HKSample 的结果列表转换为 [HKQuantitySample]

现在您有了开始和结束日期以及数量

关于ios - 从 HealthKit (HKStatistics) 获取最新(最近)的 Step 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52886981/

相关文章:

ios - 从 Xcode 6 重新构建应用程序时出现 OSStatus 错误 2003334207

android - Future <List <String>> 在迭代图像列表时返回 null

ios - 当应用程序进入后台时保持计时器运行

ios - HKHealthStore 请求AuthorizationToShareTypes :readTypes:completion always returns success?

ios - 是否可以在 HealthKit 中添加样本描述?

ios - 在 ios6 上阻止短信

ios - 我在 Xcode 7.1 中收到此错误。提交错误报告并附上 */var.. IB 代理诊断

swift - 在 Swift 中显示日期选择器的输入 - 分配给属性错误

ios - 如何从 iOS 中的音乐播放器(对于 Midi 文件)获取持续时间?

iOS : Healthkit get Oxygen Saturation in real time