HKSampleQuery 只会返回过去 7 天的值? [英] HKSampleQuery will only return values from past 7 days?

查看:34
本文介绍了HKSampleQuery 只会返回过去 7 天的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个 WatchOS 应用程序.通过测试,似乎这段代码只会返回我手动添加到健康应用程序中不到 1 周的任何体重值.这是故意的吗?绕路?

This is a WatchOS App. Through testing, it appears as though this code will only return any bodyweight values that I manually add to the health app that are less than 1 week old. Is this intended? Ways around?

func getUserBodyMass(completion: @escaping (HKQuantitySample) -> Void) {

            guard let weightSampleType = HKSampleType.quantityType(forIdentifier: .bodyMass) else {
                print("Body Mass Sample Type is no longer available in HealthKit")
                return
            }

            //1. Use HKQuery to load the most recent samples.
            let mostRecentPredicate = HKQuery.predicateForSamples(withStart: Date.distantPast,
                                                                  end: Date(),
                                                                  options: [])
            let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate,
                                                  ascending: false)
            let limit = 1
            let sampleQuery = HKSampleQuery(sampleType: weightSampleType,
                                            predicate: mostRecentPredicate,
                                            limit: limit,
                                            sortDescriptors: [sortDescriptor]) { (query, samples, error) in


                                                //2. Always dispatch to the main thread when complete.
                                                DispatchQueue.main.async {
                                                    guard let samples = samples,
                                                        let mostRecentSample = samples.first as? HKQuantitySample else {
                                                            print("getUserBodyMass sample is missing")
                                                            return
                                                    }
                                                    completion(mostRecentSample)
                                                }
            }
            healthStore.execute(sampleQuery)
    }

推荐答案

watchOS 上的 HealthKit 仅提供对最后一周数据的访问.您可以使用 HKHealthStore 方法 earliestPermittedSampleDate查询确切日期.如果您需要从 HealthKit 查询可能超过一周的历史样本,您应该使用您的配套 iOS 应用程序执行此操作,然后使用 WatchConnectivity 将相关信息发送到您的 watchOS 应用程序.

HealthKit on watchOS only provides access to the last week of data. You can use the HKHealthStore method earliestPermittedSampleDate to query for the exact date. If you need to query for historical samples from HealthKit that may be more than a week old, you should do so with your companion iOS app and then send the relevant information to your watchOS app using WatchConnectivity.

这篇关于HKSampleQuery 只会返回过去 7 天的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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