Skip to content

Commit 1194d1f

Browse files
authored
Merge pull request #6 from datlechin/fix/bug
Fix/bug
2 parents 4f48d8a + 6bdf01b commit 1194d1f

31 files changed

+5144
-4743
lines changed

OpenTable/AppDelegate.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,21 @@ class AppDelegate: NSObject, NSApplicationDelegate {
6161

6262
// Check if main window is being closed
6363
if isMainWindow(window) {
64-
// CRITICAL: Save tab state SYNCHRONOUSLY before any async operations
65-
// Otherwise sessions might be cleared before we save
66-
saveAllTabStates()
67-
68-
// NOW disconnect sessions asynchronously (after save is complete)
64+
// CRITICAL: Post notification FIRST to allow MainContentView to flush pending saves
65+
// This ensures query text is saved before SwiftUI tears down the view
66+
NotificationCenter.default.post(name: .mainWindowWillClose, object: nil)
67+
68+
// Allow run loop to process notification handlers synchronously
69+
// This is more elegant than Thread.sleep as it processes pending events
70+
// rather than blocking the main thread entirely
71+
RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.05))
72+
73+
// NOTE: We do NOT call saveAllTabStates() here because:
74+
// 1. MainContentView already flushed the correct state via the notification above
75+
// 2. By this point, SwiftUI may have torn down views and session.tabs could be stale/empty
76+
// 3. Saving again would risk overwriting the good state with bad/empty state
77+
78+
// Disconnect sessions asynchronously (after save is complete)
6979
Task { @MainActor in
7080
await DatabaseManager.shared.disconnectAll()
7181
}

0 commit comments

Comments
 (0)