如何使用 Swift 从 iOS HealthKit 中逐分钟读取数据

发布于 2025-01-13 13:17:11 字数 1838 浏览 4 评论 0原文

我每小时、每天、每周等都正确地从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文