Fix timer crash by ensuring Timer is scheduled on main thread

- Timer.scheduledTimer must be called on main thread with run loop
- Wrapped timer scheduling in DispatchQueue.main.async
- This prevents EXC_BREAKPOINT crash when called from background threads
This commit is contained in:
jack
2025-07-04 16:52:57 +02:00
parent 25de7df10e
commit c5041941e3
2 changed files with 17 additions and 13 deletions
+11 -8
View File
@@ -782,17 +782,20 @@ class BluetoothMeshService: NSObject {
self.delegate?.didUpdatePeerList(connectedPeerIDs)
}
} else {
// Cancel any pending update
peerListUpdateTimer?.invalidate()
// Schedule a new update after debounce interval
peerListUpdateTimer = Timer.scheduledTimer(withTimeInterval: peerListUpdateDebounceInterval, repeats: false) { [weak self] _ in
// Must schedule timer on main thread
DispatchQueue.main.async { [weak self] in
guard let self = self else { return }
let connectedPeerIDs = self.getAllConnectedPeerIDs()
// print("[DEBUG] Notifying peer list update after debounce: \(connectedPeerIDs.count) peers")
// Cancel any pending update
self.peerListUpdateTimer?.invalidate()
DispatchQueue.main.async {
// Schedule a new update after debounce interval
self.peerListUpdateTimer = Timer.scheduledTimer(withTimeInterval: self.peerListUpdateDebounceInterval, repeats: false) { [weak self] _ in
guard let self = self else { return }
let connectedPeerIDs = self.getAllConnectedPeerIDs()
// print("[DEBUG] Notifying peer list update after debounce: \(connectedPeerIDs.count) peers")
self.delegate?.didUpdatePeerList(connectedPeerIDs)
}
}