Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从HealthKit数据中获取定义时间间隔内每天的最新体重输入_Ios_Swift_Swift4_Healthkit - Fatal编程技术网

Ios 如何从HealthKit数据中获取定义时间间隔内每天的最新体重输入

Ios 如何从HealthKit数据中获取定义时间间隔内每天的最新体重输入,ios,swift,swift4,healthkit,Ios,Swift,Swift4,Healthkit,您好,我想在定义的时间间隔内获取每天体重的最新数据点 (就我而言,我每隔一周需要一次,但每天只需要最后一次。) 实际上,使用这段代码我可以得到从开始X日期到结束X日期的所有条目 let query = HKSampleQuery(sampleType: type!, predicate: predicate, limit: 0, sortDescriptors: nil, resultsHandler: { (quer

您好,我想在定义的时间间隔内获取每天体重的最新数据点 (就我而言,我每隔一周需要一次,但每天只需要最后一次。)

实际上,使用这段代码我可以得到从开始X日期到结束X日期的所有条目

let query = HKSampleQuery(sampleType: type!, predicate: predicate, 
                                      limit: 0, sortDescriptors: nil, resultsHandler: { (query, results, error) in  
                                        if let myResults = results { 
                                            for result in myResults { 
                                                let bodymass = result as! HKQuantitySample 
                                                let weight = bodymass.quantity.doubleValue(for: unit) 
                                                Print ("this is my weight value",weight  ) 
                                            } 
                                        } 
                                        else { 
                                            print("There was an error running the query: \(String(describing: error))") 
                                        } 
此查询返回在时间范围内测量所消耗重量的所有样本。 我只想返回记录的最后一个条目有什么方法可以用heath kit查询来完成吗

我试图定义排序描述符,但没有找到一种方法使其在定义的时间间隔内工作

谢谢


正如您所说,要使用排序描述符,只需使用
Date.distantPast
Date()
作为范围,然后只需获取第一个:

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)
    }