admin管理员组文章数量:1356959
I have a basic step counter app that returns the last weeks' step counts. Today is March 29th, yet the app returns today count as March 21st.
Any thoughts on what's going on?
(I will attach a png of the results if I can). Here is the code that generates that results.
func calculateSteps() async throws {
guard let healthStore = self.healthStore else { return }
let startDate = Calendar.current.date(byAdding: .day, value: -7, to: .startOfDay)
let endDate = Date()
let stepType = HKQuantityType(.stepCount)
let daily = DateComponents(day: 1)
let thisWeek = HKQuery.predicateForSamples(withStart: startDate, end: endDate)
let stepsThisWeek = HKSamplePredicate.quantitySample(type: stepType, predicate: thisWeek)
let sumOfSteps = HKStatisticsCollectionQueryDescriptor(
predicate: stepsThisWeek,
options: .cumulativeSum,
anchorDate: endDate,
intervalComponents: daily
)
let stepsCount = try await sumOfSteps.result(for: healthStore)
guard let startDate = startDate else { return }
stepsCount.enumerateStatistics(from: startDate, to: endDate) { results, end in
let count = results.sumQuantity()?.doubleValue(for: .count())
let step = Step(count: Int(count ?? 0), date: results.startDate)
if step.count > 0 {
self.steps.append(step)
}
}
}
}
Thanks. Blessings, --Mark
I have a basic step counter app that returns the last weeks' step counts. Today is March 29th, yet the app returns today count as March 21st.
Any thoughts on what's going on?
(I will attach a png of the results if I can). Here is the code that generates that results.
func calculateSteps() async throws {
guard let healthStore = self.healthStore else { return }
let startDate = Calendar.current.date(byAdding: .day, value: -7, to: .startOfDay)
let endDate = Date()
let stepType = HKQuantityType(.stepCount)
let daily = DateComponents(day: 1)
let thisWeek = HKQuery.predicateForSamples(withStart: startDate, end: endDate)
let stepsThisWeek = HKSamplePredicate.quantitySample(type: stepType, predicate: thisWeek)
let sumOfSteps = HKStatisticsCollectionQueryDescriptor(
predicate: stepsThisWeek,
options: .cumulativeSum,
anchorDate: endDate,
intervalComponents: daily
)
let stepsCount = try await sumOfSteps.result(for: healthStore)
guard let startDate = startDate else { return }
stepsCount.enumerateStatistics(from: startDate, to: endDate) { results, end in
let count = results.sumQuantity()?.doubleValue(for: .count())
let step = Step(count: Int(count ?? 0), date: results.startDate)
if step.count > 0 {
self.steps.append(step)
}
}
}
}
Thanks. Blessings, --Mark
Share Improve this question asked Mar 29 at 21:35 GarageshopGarageshop 12110 bronze badges1 Answer
Reset to default 0I discovered I had the date: wrong. I had it as results.startDate, and when I flipped it to resuluts.endDate, the data produced was correct.
Thanks.
Blessings, --Mark
本文标签: hkhealthstoreSwiftUI HKHeathStore not returning today39s date with dataStack Overflow
版权声明:本文标题:hkhealthstore - SwiftUI HKHeathStore not returning today's date with data - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744002924a2574191.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论