Apple Watch HeathKit observe Workout-App workout finished

Viewed 94

For my App on Apple Watch I want to observe when a workout in Workout-App was completed, to do a background update of my App and re-render the Complication. But the Query never fires.

My Approach was to use an HKObserverQuery and observe .workoutType(). Documentation mentions:

HealthKit uses workout types to create samples that store information about individual workouts.

On https://developer.apple.com/documentation/healthkit/hkobjecttype/1615132-workouttype. So that felt like the right sampleType.

I added com.apple.developer.healthkit.background-delivery to the Entitlements of the WatchApp Extension and Workout access permissions are setup properly.

That is the code, I am using:

let query = HKObserverQuery(sampleType: .workoutType(), predicate: nil, updateHandler: { [weak self] _, completionHandler, error in
    guard error == nil else {
//    CoreInsights.logs.track("HK Query Error", level: .error)
        return
    }
//    CoreInsights.logs.track("Update Trigger HK", level: .info)
//    self?.performBackgroundTasks(completion: {
//        completionHandler()
//    })
})

self?.healthStore.execute(query)
self?.healthStore.enableBackgroundDelivery(for: .workoutType(), frequency: .immediate, withCompletion: { updateSuccess, _ in
//    CoreInsights.logs.track("HK Background \(updateSuccess)", level: .info)
})

healthStore.enableBackgroundDelivery returns with success true.

Someone knows, if I am using the right type and setup background correctly?

0 Answers
Related