How can I get the most recent weight entry from healthkit data?
My code is only returning the first weight entry ever recorded.
Is is possible to get
Your query currently doesn't specify any sort descriptors. You'll need to specify sort descriptors in order to get the query results in the order you expect. You can read more about them in the HKSampleQuery
documentation.
Thanks to @Allan answer, I return the last recorded entry by specifying a sortDescriptor:
let sortDescriptor = NSSortDescriptor(key: HKSampleSortIdentifierStartDate, ascending: false)
let query = HKSampleQuery(sampleType: bodyMassType, predicate: nil, limit: 1, sortDescriptors: [sortDescriptor]) { (query, results, error) in
...
}