gpt4 book ai didi

ios - Swift - Healthkit 的 HKSampleQuery 示例

转载 作者:可可西里 更新时间:2023-11-01 01:50:24 24 4
gpt4 key购买 nike

我正在尝试返回最近 7 天记录的心跳样本。我认为我的方法不错,但我仍然不太确定我的查询部分。这是我到目前为止所做的。唯一被打印的想法是

95 count/min 90EBAB07-9CAB-445E-A967-4BD5E8AADAA4 "User’s Apple Watch" (5.1.3), "Watch3,2" (5.1.3)"Apple Watch" metadata: {
HKMetadataKeyHeartRateMotionContext = 0;
} (2019-02-22 21:51:31 -0400 - 2019-02-22 21:51:31 -0400)

这是我的查询:

    func getTodaysHeartRate(completion: (@escaping (HKQuantitySample) -> Void)) {

let sortDescriptor = NSSortDescriptor(
key: HKSampleSortIdentifierEndDate,
ascending: false)

let heartRateType = HKSampleType.quantityType(forIdentifier: .heartRate)!
let endDate = Date()
let startDate = endDate.addingTimeInterval(-1.0 * 60 * 60 * 24)
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: [])

let query = HKSampleQuery(sampleType: heartRateType, predicate: predicate, limit: 0, sortDescriptors: [sortDescriptor])
{ (query, result, error) -> Void in
DispatchQueue.main.async {
guard let result = result,
let resultCount = result.first as? HKQuantitySample else {
print("Failed to fetch heart rate")
return
}
completion(resultCount)
}
}
healthStore.execute(query)
}

@IBOutlet weak var heartRate: UILabel!
@IBAction func getHeartRate(_ sender: Any) {
getTodaysHeartRate { (result) in
print("\(result)")
DispatchQueue.main.async {
self.heartRate.text = "\(result)"
}
}

最佳答案

这应该会返回过去 7 天的数据。希望对您有所帮助!

let startDate = Date() - 7 * 24 * 60 * 60
let endDate = Date()
let predicate = HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: [])

关于ios - Swift - Healthkit 的 HKSampleQuery 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54847629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com