我正在尝试使用复合谓词从iOS HealthKit获取HeartRate数据。但似乎没有提供任何数据。
这是以下谓词代码
let predicate = NSCompoundPredicate(type: NSCompoundPredicate.LogicalType.and, subpredicates: [HKQuery.predicateForSamples(withStart: startDate, end: endDate, options: .strictEndDate), HKQuery.predicateForObjects(withDeviceProperty: HKMetadataKeyHeartRateMotionContext, allowedValues: ["1"])])
var interval = DateComponents()
interval.day = 1
let query = HKStatisticsCollectionQuery(quantityType: type, quantitySamplePredicate: predicate, options: [.discreteAverage], anchorDate: startDate, intervalComponents:interval)
query.initialResultsHandler = { query, results, error in
if error != nil {
return
}
DispatchQueue.main.async {
completionHandler(results)
}
}
HKSingleton.sharedInstance.healthStore.execute(query)
HKMetadataKeyHeartRateMotionContext 是测量心率样本时用户的活动水平。
如果我在此代码中做错了什么,请纠正我。
预先感谢