首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HKSampleQuery只会返回过去7天的值?

HKSampleQuery只会返回过去7天的值?
EN

Stack Overflow用户
提问于 2018-02-10 05:35:36
回答 1查看 2.8K关注 0票数 4

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

代码语言:javascript
复制
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)
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-12 16:19:37

HealthKit on watchOS只提供对最后一周数据的访问。您可以使用HKHealthStore方法earliestPermittedSampleDate查询确切日期。如果您需要查询来自HealthKit的历史样本,这些样本可能已经超过一周了,您应该使用您的同伴iOS应用程序来查询,然后使用WatchConnectivity将相关信息发送到您的watchOS应用程序。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48717614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档