最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

hkhealthstore - SwiftUI HKHeathStore not returning today's date with data - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

I 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

发布评论

评论列表(0)

  1. 暂无评论