From 46c8143bba21deba815472a1278f5a674370971a Mon Sep 17 00:00:00 2001 From: liwei1dao Date: Mon, 15 Dec 2025 16:47:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=B6=85=E6=97=B6=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E4=B8=8D=E8=B5=B7=E4=BD=9C=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../agent_service/AgentServiceImpl.swift | 29 +++++-------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift b/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift index b861e6d1d..bd9a17a1e 100644 --- a/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift +++ b/local_plugins/agent_service/ios/agent_service/Sources/agent_service/AgentServiceImpl.swift @@ -386,40 +386,29 @@ class AgentServiceImpl: NSObject { } private func startIdleCheck(idleSeconds: TimeInterval = 10) { - // 2. 所有对idleTimer的操作都通过串行队列执行,避免竞态条件 timerQueue.async { - // 先停止旧的定时器(在串行队列中执行,确保线程安全) - self.stopIdleCheck() - + if self.idleTimer != nil { + self.idleTimer?.cancel() + self.idleTimer = nil + } guard self.isRecognizing else { return } - os_log("启动空闲检测,超时时间: %.0f秒,模式: %{public}@", log: self.logger, type: .info, idleSeconds, self.currentRecognitionMode) - - // 使用全局队列作为定时器回调队列(避免后台挂起) let timer = DispatchSource.makeTimerSource(queue: DispatchQueue.global(qos: .utility)) - timer.schedule(deadline: .now() + idleSeconds) timer.setEventHandler { [weak self] in guard let self = self else { return } - - // 回调在全局队列,涉及UI或实例变量操作时切换到主线程 DispatchQueue.main.async { [weak self] in guard let self = self else { return } - - // 检查状态(确保在主线程访问这些属性,避免UI相关竞态) + os_log("空闲检测触发自动停止,模式: %{public}@", + log: self.logger, + type: .info, + self.currentRecognitionMode) if self.isRecognizing && !self.isSpeaking && !self.isAiStreaming { - os_log("空闲检测触发自动停止,模式: %{public}@", - log: self.logger, - type: .info, - self.currentRecognitionMode) - - // 关闭编码器(设备侧操作,若BleService线程安全可直接调用,否则需加锁) BleService.shared.closeCodec() - self.stopRecognition() self.sendEvent(name: "auto_stop", data: [ "reason": "idle_timeout", @@ -429,9 +418,7 @@ class AgentServiceImpl: NSObject { } } } - timer.resume() - // 3. 在串行队列中安全赋值 self.idleTimer = timer } }