如何打印刚刚记录并保存到Healthkit商店的HKQuantity类型的值?

问题描述 投票:0回答:1

我正在与HKStore分享锻炼,我已经要求阅读以下类型:

let typesToRead: Set = [
    HKQuantityType.quantityType(forIdentifier: .heartRate)!,
    HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)!,
    HKQuantityType.quantityType(forIdentifier: .distanceWalkingRunning)!
]

在我的训练课程(跟踪和记录活动锻炼)中,我能够记录实时数据并将其保存到HealthStore,但我不知道如何读取该数据并将其显示在屏幕上以供用户在那里查看实时锻炼。 (或者至少在控制台中打印心率,activeEnergyBurned等数据)。

这是HKLiveWorkoutBuilderDelegate,似乎正在共享相关的锻炼数据

// MARK: HKLiveWorkoutBuilderDelegate
func workoutBuilder(_ workoutBuilder: HKLiveWorkoutBuilder, didCollectDataOf collectedTypes: Set<HKSampleType>) {
    for type in collectedTypes {
        guard let quantityType = type as? HKQuantityType else {
            return // Nothing to do
        }

        let statistics = workoutBuilder.statistics(for: quantityType)
        let typeDescription = type.description

    }
}

以下是我的锻炼方式:

    override func awake(withContext context: Any?) {
        super.awake(withContext: context)

        configuration.activityType = .running
        configuration.locationType = .indoor

        do {
            session = try HKWorkoutSession(healthStore: healthStore, configuration: configuration)
            builder = session.associatedWorkoutBuilder()
        } catch {
            dismiss()
            return
        }

        // Setup session and builder
        session.delegate = self
        builder.delegate = self
        builder.dataSource = HKLiveWorkoutDataSource(healthStore: healthStore, workoutConfiguration: configuration)


        // Start session and builder
        session.startActivity(with: Date())
        builder.beginCollection(withStart: Date()) { (success, error) in
            self.setDurationTimerDate()
        }
    }

注意:我正在使用Apples新的Beta WatchOS 5.0

swift health-kit watch-os heartbeat
1个回答
1
投票

您正在完成大部分工作,但您还需要启用向dataSource收集数据。在我的应用程序中,我收集距离和心率数据,我这样做:

let dataSource = HKLiveWorkoutDataSource(healthStore: healthStore, workoutConfiguration: configuration)

if let hr = HKQuantityType.quantityType(forIdentifier: .heartRate) {
    dataSource.enableCollection(for: hr, predicate: nil)
}
if let distance = HKQuantityType.quantityType(forIdentifier: .distanceWalkingRunning) {
    dataSource.enableCollection(for: distance, predicate: nil)
}

workoutBuilder?.dataSource = dataSource

然后我可以在HKLiveWorkoutBuilderDelegate方法中打印这些信息,如下所示:

func workoutBuilder(_ workoutBuilder: HKLiveWorkoutBuilder, didCollectDataOf collectedTypes: Set<HKSampleType>) {

    guard let hrType = HKQuantityType.quantityType(forIdentifier: .heartRate),
        let distanceType = HKQuantityType.quantityType(forIdentifier: .distanceWalkingRunning) else {
            return
    }

    if collectedTypes.contains(hrType) {
        if let hrQuantity = workoutBuilder.statistics(for: hrType)?.mostRecentQuantity() {
            // We want to have BPM
            let hrUnit = HKUnit(from: "count/min")
            let hr = Int(hrQuantity.doubleValue(for: hrUnit))

            debugPrint("HR: \(hr)")
        }
    }

    if collectedTypes.contains(distanceType) {
        if let distQuantity = workoutBuilder.statistics(for: distanceType)?.sumQuantity() {
            // We want to have total distance in meters
            let distance = distQuantity.doubleValue(for: HKUnit.meter())

            debugPrint("Distance: \(distance) m")
        }
    }
}

然后我进入控制台:

"Distance: 6.5 m"
"Distance: 10.4 m"
"HR: 112"
"Distance: 14.3 m"
"HR: 117"
"Distance: 20.8 m"
© www.soinside.com 2019 - 2024. All rights reserved.