0

Hello I want to grab the latest datapoint for body mass of each day in a defined time interval (in my case I need it for an interval of one week but only the last entry for each day.)

Actually, Using this code I can get all the entries from start X date to the end X date

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

This query returns any samples measuring weight consumed that fall within the time frame. I just want to return the last entry recorded is there any way to do it with heath-kit query?

I've tried to define sort descriptor but I don’t find a way to make it work with defined time interval.

Thanks

I read this and this one

sarra.srairi
  • 150
  • 14

1 Answers1

1

As you said you want to use a sort descriptor, just use Date.distantPast and Date() as your range, then just grab the first:

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)
    }
GarySabo
  • 5,806
  • 5
  • 49
  • 124