mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 13:45:20 +00:00
Fix crash from duplicate peer IDs in Dictionary initialization
- Added deduplication logic in ChatViewModel to handle duplicate peer IDs gracefully - Enhanced PeerManager to prevent duplicate peers by tracking both nicknames and IDs - Added final safety check to ensure no duplicates in peers array - This fixes crash when Dictionary(uniqueKeysWithValues:) encounters duplicate keys
This commit is contained in:
@@ -220,7 +220,18 @@ class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
// Update peers directly
|
||||
self?.allPeers = peers
|
||||
// Update peer index for O(1) lookups
|
||||
self?.peerIndex = Dictionary(uniqueKeysWithValues: peers.map { ($0.id, $0) })
|
||||
// Deduplicate peers by ID to prevent crash from duplicate keys
|
||||
var uniquePeers: [String: PeerData] = [:]
|
||||
for peer in peers {
|
||||
// Keep the first occurrence of each peer ID
|
||||
if uniquePeers[peer.id] == nil {
|
||||
uniquePeers[peer.id] = peer
|
||||
} else {
|
||||
SecureLogger.log("⚠️ Duplicate peer ID detected: \(peer.id) (\(peer.displayName))",
|
||||
category: SecureLogger.session, level: .warning)
|
||||
}
|
||||
}
|
||||
self?.peerIndex = uniquePeers
|
||||
// Schedule UI update if peers changed
|
||||
if peers.count > 0 || self?.allPeers.count ?? 0 > 0 {
|
||||
// UI will update automatically
|
||||
|
||||
Reference in New Issue
Block a user