Skip to content

Commit e06ded6

Browse files
authored
fix: connection list mismatch after iCloud sync (#516) (#517)
ConnectionStorage.saveConnections() set cache BEFORE writing to UserDefaults. If encoding failed or a race condition occurred, cache diverged from disk. All consumers (welcome window, dock menu, connection switcher) read stale data. Fix: clear cache AFTER successful write (matching GroupStorage/TagStorage pattern) and invalidate cache before applying remote sync changes.
1 parent 7deb5b1 commit e06ded6

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

TablePro/Core/Storage/ConnectionStorage.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,22 @@ final class ConnectionStorage {
5050

5151
/// Save all connections
5252
func saveConnections(_ connections: [DatabaseConnection]) {
53-
cachedConnections = connections
54-
5553
let storedConnections = connections.map { StoredConnection(from: $0) }
5654

5755
do {
5856
let data = try encoder.encode(storedConnections)
5957
defaults.set(data, forKey: connectionsKey)
58+
cachedConnections = nil
6059
} catch {
6160
Self.logger.error("Failed to save connections: \(error)")
6261
}
6362
}
6463

64+
/// Invalidate the in-memory cache so the next load reads fresh from UserDefaults.
65+
func invalidateCache() {
66+
cachedConnections = nil
67+
}
68+
6569
/// Add a new connection
6670
func addConnection(_ connection: DatabaseConnection, password: String? = nil) {
6771
var connections = loadConnections()

TablePro/Core/Sync/SyncCoordinator.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,9 @@ final class SyncCoordinator {
379379
private func applyRemoteChanges(_ result: PullResult) {
380380
let settings = AppSettingsStorage.shared.loadSync()
381381

382+
// Invalidate caches before applying remote data to ensure fresh reads
383+
ConnectionStorage.shared.invalidateCache()
384+
382385
// Suppress change tracking during remote apply to avoid sync loops
383386
changeTracker.isSuppressed = true
384387
defer {

0 commit comments

Comments
 (0)