Sorry, @HealthyPotter I missed part of the code earlier. In fact, the error scenario also has a callback. let query = HKObserverQuery(sampleType: heartType, predicate: nil) { query, completionHandler, error in if let error = error { logEvent(Observer 错误:(error.localizedDescription)) completionHandler() return } logEvent(【心率有变化了) MyNotificationManager.shared.sendNotification() // 发了一个本地通知 completionHandler() } healthStore.execute(query) healthStore.enableBackgroundDelivery(for: heartType, frequency: .hourly) { success, error in if success { logEvent(已启用后台心率监听) } else { logEvent(后台监听心率失败:(error?.localizedDescription ?? 未知错误)) } } 】
Topic:
App & System Services
SubTopic:
Health & Fitness
Tags: