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

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

问题描述

这是一个WatchOS应用程序.通过测试,该代码似乎只会返回我手动添加到Health App中的任何体重值,这些体重值还不到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天全站免登陆