How to get the most recent Weight entry from HealthKit data

后端 未结 2 1067
迷失自我
迷失自我 2021-01-12 19:37

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

相关标签:
2条回答
  • 2021-01-12 20:25

    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.

    0 讨论(0)
  • 2021-01-12 20:34

    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
            ...
        }
    
    0 讨论(0)
提交回复
热议问题