func getStepRecords(from startDate: Date, to endDate: Date) async -> [StepData] {
var recordStepDatas = [StepData]()
let statistics = await getStepRecordsOneDays(from: startDate, to: endDate)
statistics.forEach { statistic in
let steps = statistic.sumQuantity()?.doubleValue(for: HKUnit.count()) ?? 0
recordStepDatas.append(
StepData(
steps: steps,
startedAt: statistic.startDate,
endedAt: statistic.endDate
)
)
}
return recordStepDatas
}
func getStepRecordsOneDays(from startDate: Date, to endDate: Date) async -> [HKStatistics] {
let intervals = DateComponents(day: 1)
let anchorDate = calendar.date(bySettingHour: 0, minute: 0, second: 0, of: .now) ?? .now
return await withCheckedContinuation { continuation in
let query = HKStatisticsCollectionQuery.init(quantityType: stepCountType, quantitySamplePredicate: nil, options: .cumulativeSum, anchorDate: anchorDate, intervalComponents: intervals)
var staticsArray = [HKStatistics]()
query.initialResultsHandler = { _, results, _ in
results?.enumerateStatistics(from: startDate, to: endDate) { result, _ in
staticsArray.append(result)
}
continuation.resume(returning: staticsArray)
}
store.execute(query)
}
I recently ran into a similar problem again.
Step count data has not been retrieved properly since a certain point in time.
This issue does not seem to occur for all users, but only occasionally for a small number of users.
Is there anything in my code that might be problematic?
Topic:
Community
SubTopic:
Apple Developers
Tags: