workoutbuilder и workoutbuilderDidCollectEvent никогда не называются - PullRequest
0 голосов
/ 08 ноября 2019

Я прошел учебное пособие по созданию сеанса тренировки, но эти две функции никогда не вызывались.

Это все в часах приложения

let healthStore = HKHealthStore()
var configuration: HKWorkoutConfiguration!
var session: HKWorkoutSession!
var builder: HKLiveWorkoutBuilder!

override func awake(withContext context: Any?) {
    super.awake(withContext: context)
let typesToShare: Set = [
        HKQuantityType.workoutType()
    ]

    // The quantity types to read from the health store.
    let typesToRead: Set = [
        HKQuantityType.quantityType(forIdentifier: .heartRate)!,
        HKQuantityType.quantityType(forIdentifier: .activeEnergyBurned)!,
        HKQuantityType.quantityType(forIdentifier: .distanceWalkingRunning)!
    ]

    // Request authorization for those quantity types.
    healthStore.requestAuthorization(toShare: typesToShare, read: typesToRead) { (success, error) in
        // Handle errors here.
    }

    configuration = HKWorkoutConfiguration()
    configuration.activityType = .running
    configuration.locationType = .outdoor

    do {
        session = try HKWorkoutSession(healthStore: healthStore, configuration: configuration)
        builder = session.associatedWorkoutBuilder()
    } catch {
        dismiss()
        return
    }
    builder.dataSource = HKLiveWorkoutDataSource(healthStore: healthStore,
    workoutConfiguration: configuration)
    session.startActivity(with: Date())
    builder.beginCollection(withStart: Date()) { (success, error) in
    }
}

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

        // Calculate statistics for the type.
        let statistics = workoutBuilder.statistics(for: quantityType)
        //let label = labelForQuantityType(quantityType)

        DispatchQueue.main.async() {
            // Update the user interface.
            let heartRateUnit = HKUnit.count().unitDivided(by: HKUnit.minute())
            let value = statistics?.mostRecentQuantity()?.doubleValue(for: heartRateUnit)
            let roundedValue = Double( round( 1 * value! ) / 1 )
            print("\(roundedValue) BPM")
        }
    }
}



func workoutBuilderDidCollectEvent(_ workoutBuilder: HKLiveWorkoutBuilder) {

    let lastEvent = workoutBuilder.workoutEvents.last

    DispatchQueue.main.async() {
        // Update the user interface here.
        print(lastEvent)

    }
}

Ничего не печатается, и нетточка останова внутри этих двух функций когда-либо поражена. Что мне не хватает? Кажется, что сессия запущена, так как есть маленькая иконка человеческого бега, когда я минимизирую приложение

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...