我正试图返回最近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)"
}
}
答案 0 :(得分:1)
这应该在最近7天返回您。希望对您有帮助!
TIMESTAMP