如何使用 Swift 从 iOS HealthKit 中逐分钟读取数据
我每小时、每天、每周等都正确地从 HealthKit 读取步数数据。但是当将查询时间间隔减少到 15 分钟以下时,HealthKit 返回错误的步数数据。我需要一分钟一分钟地读取数据。是否可以?
这是我的代码。 HealthKit 使用下面的代码返回数据,但它显示的步数数据超过 10%。
func getMinutelyStepData(startingDate: Date) {
print("Setting hourly step data for \(startingDate)")
var loopCount = 0
var totalStep = 0.0
let endOfDay = startingDate.endOfDay//Calendar.current.date(byAdding: .hour, value: 23, to: startingDate)!
let stepType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
let minutely = DateComponents(minute: 1)
var queryStepMinutely: HKStatisticsCollectionQuery?
let predicate = HKQuery.predicateForSamples(withStart: startingDate, end: endOfDay, options: .strictStartDate)
let myAnchorDate = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: Date())!
queryStepMinutely = HKStatisticsCollectionQuery(quantityType: stepType, quantitySamplePredicate: predicate, options: .cumulativeSum, anchorDate: myAnchorDate, intervalComponents: minutely)
queryStepMinutely!.initialResultsHandler = { query, statisticsCollection, error in
if let result = statisticsCollection {
result.enumerateStatistics(from: startingDate, to: endOfDay) { (statistics, stop) in
if let step = statistics.sumQuantity() {
let value = step.doubleValue(for: .count())
totalStep += value
print("Minutely step data: \(value), \(statistics.startDate), my total step: \(totalStep)")
} else {
print("No step data for minutely available, so add 0..., \(statistics.startDate), \(statistics.endDate)")
}
}
}
}
myHealthStore.execute(queryStepMinutely!)
}
I'm correctly reading step data from HealthKit hourly, daily, weekly, etc. But when it comes to reducing the query time interval to less than 15 minutes, HealthKit returns wrong step data. I need to read data minute by minute. Is it possible?
Here is my code. HealthKit returning data by using the code below, but it shows step data more than 10 percent.
func getMinutelyStepData(startingDate: Date) {
print("Setting hourly step data for \(startingDate)")
var loopCount = 0
var totalStep = 0.0
let endOfDay = startingDate.endOfDay//Calendar.current.date(byAdding: .hour, value: 23, to: startingDate)!
let stepType = HKQuantityType.quantityType(forIdentifier: HKQuantityTypeIdentifier.stepCount)!
let minutely = DateComponents(minute: 1)
var queryStepMinutely: HKStatisticsCollectionQuery?
let predicate = HKQuery.predicateForSamples(withStart: startingDate, end: endOfDay, options: .strictStartDate)
let myAnchorDate = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: Date())!
queryStepMinutely = HKStatisticsCollectionQuery(quantityType: stepType, quantitySamplePredicate: predicate, options: .cumulativeSum, anchorDate: myAnchorDate, intervalComponents: minutely)
queryStepMinutely!.initialResultsHandler = { query, statisticsCollection, error in
if let result = statisticsCollection {
result.enumerateStatistics(from: startingDate, to: endOfDay) { (statistics, stop) in
if let step = statistics.sumQuantity() {
let value = step.doubleValue(for: .count())
totalStep += value
print("Minutely step data: \(value), \(statistics.startDate), my total step: \(totalStep)")
} else {
print("No step data for minutely available, so add 0..., \(statistics.startDate), \(statistics.endDate)")
}
}
}
}
myHealthStore.execute(queryStepMinutely!)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论