I am using this below code since WatchOS 10 to set the user steps observer and get the callback of steps whenever changes.
This is still working perfectly fine till watchOS 11 but when i updated to watchOS 26.1, I am not getting the callback of steps, like the observer is not working at all. I should get a callback inside query block whenever user take steps, but it is not working in watchOS 26.1.
func setupStepCountObserver(completion: @escaping (Double, Double) -> Void) { let stepCountType = HKObjectType.quantityType(forIdentifier: .stepCount)!
let query = HKObserverQuery(sampleType: stepCountType, predicate: nil) { [weak self] _, completionHandler, error in
if let error = error {
print("Error setting up observer query: \(error.localizedDescription)")
return
}
// Fetch the latest step count data
self?.getLast20SecTodaysSteps(completion: completion)
// Call the completion handler to let HealthKit know you have processed the update
completionHandler()
}
// Execute the query
healthStore.execute(query)
// Enable background delivery of updates
healthStore.enableBackgroundDelivery(for: stepCountType, frequency: .immediate) { success, error in
if let error = error {
print("Error enabling background delivery steps: \(error.localizedDescription)")
} else if success {
print("Background delivery enabled for steps.")
}
}
}