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