作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我想在定义的时间间隔中获取每天体重的最新数据点(在我的例子中,我需要它间隔一周,但只需要每天的最后一个条目。)
实际上,使用这段代码我可以获得从开始 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)
}
关于ios - 如何在定义的时间间隔内每天从 HealthKit 数据中获取最新的体重条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46712731/
我希望该按钮(子项)占水平 LinearLayout(其父项)全宽的 25%。代码: LinearLayout ll = new LinearLayout(this); ll.setLayoutPar
我想问一下,当循环开始并再次循环时,字符串变量名称会增加1,我该怎么做。这个程序应该问你要写多少个病人。如果你为前任写信。 10,那么循环将进行10次,它会询问我想要的所有信息,然后将它们添加到我已经
我是一名优秀的程序员,十分优秀!