简体   繁体   English

如何从 HKStatistics 创建 HKQuantitySample?

[英]How to create an HKQuantitySample from HKStatistics?

I'm trying to process HKStatistics that are heart rates and create an HKQuantitySample so that I can append to a local array.我试图过程HKStatistics是心脏速率,并创建一个HKQuantitySample ,这样我可以追加到本地阵列。 I can't figure out what dates to use for startDate and endDate , currently I'm just using Date() but I would rather be more precise and use the actual sample's date.我不知道用于startDateendDate日期,目前我只是使用Date()但我宁愿更精确并使用实际样本的日期。 statistics.StartDate just gives you the date that the statistic collection started, but not the date of the individual sample/statistic. statistics.StartDate只为您提供统计数据收集开始的日期,而不是单个样本/统计数据的日期。 I see that the statistic also has a mostRecentQuantityDateInterval() property but not sure how to use it here.我看到统计数据也有一个mostRecentQuantityDateInterval()属性,但不知道如何在这里使用它。

 private func processStatistics(withStatistics statistics: HKStatistics?) {
        
        // Make sure we got non `nil` parameters.
        guard let statistics = statistics else {
            fatalError("no statistics in processStatistics")
        }
        
        
        // Dispatch to main, because we are updating the interface.
        DispatchQueue.main.async {
            switch statistics.quantityType {
            case HKQuantityType.quantityType(forIdentifier: .heartRate):
                
                if let unwrappedQuantity = statistics.mostRecentQuantity() {
                    let statisticAsHKQuantitySample = HKQuantitySample(type: statistics.quantityType, quantity: unwrappedQuantity, start: Date(), end: Date())
                    
                    self.heartRateSamples.append(statisticAsHKQuantitySample)
                }

I think you're looking for: mostRecentQuantityDateInterval()我认为您正在寻找: mostRecentQuantityDateInterval()

let interval = statistics.mostRecentQuantityDateInterval()
interval.start
interval.end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM