Commit 9fef98f
Fix SSE reconnection when app returns to foreground (#217)
## Problem
When users navigated away from the app during an SSE connection to a
codespace, iOS would suspend the URLSession connection. Upon returning
to the app, the connection would remain stuck in a "connecting" state
indefinitely, never completing or showing an error.
## Solution
Added app lifecycle monitoring using SwiftUI's `scenePhase` environment
value. When the app is backgrounded during an active SSE connection, the
view now tracks this state. Upon returning to the foreground, it
automatically disconnects the stale connection and initiates a fresh SSE
connection to the same codespace.
## Implementation
- Added `@Environment(\.scenePhase)` to monitor app state transitions
- Tracks connection state with `wasConnectingBeforeBackground` flag
- Stores `pendingCodespaceName` to preserve the target codespace across
app backgrounding
- Implements `handleScenePhaseChange()` to detect background→active
transitions and trigger reconnection
- Uses SwiftUI-native approach instead of NotificationCenter for better
integration with view lifecycle
---------
Co-authored-by: Claude <claude@anthropic.com>1 parent 3304361 commit 9fef98f
File tree
5 files changed
+70
-23
lines changed- xcode
- Catnip.xcodeproj
- catnip
- Views
5 files changed
+70
-23
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
423 | 423 | | |
424 | 424 | | |
425 | 425 | | |
| 426 | + | |
426 | 427 | | |
427 | 428 | | |
428 | | - | |
| 429 | + | |
429 | 430 | | |
430 | 431 | | |
431 | 432 | | |
| |||
437 | 438 | | |
438 | 439 | | |
439 | 440 | | |
440 | | - | |
| 441 | + | |
441 | 442 | | |
| 443 | + | |
442 | 444 | | |
443 | 445 | | |
444 | 446 | | |
| |||
456 | 458 | | |
457 | 459 | | |
458 | 460 | | |
| 461 | + | |
459 | 462 | | |
460 | 463 | | |
461 | | - | |
| 464 | + | |
462 | 465 | | |
463 | 466 | | |
464 | 467 | | |
| |||
470 | 473 | | |
471 | 474 | | |
472 | 475 | | |
473 | | - | |
| 476 | + | |
474 | 477 | | |
| 478 | + | |
475 | 479 | | |
476 | 480 | | |
477 | 481 | | |
| |||
490 | 494 | | |
491 | 495 | | |
492 | 496 | | |
| 497 | + | |
493 | 498 | | |
494 | 499 | | |
495 | | - | |
| 500 | + | |
496 | 501 | | |
497 | 502 | | |
498 | 503 | | |
| 504 | + | |
| 505 | + | |
499 | 506 | | |
500 | 507 | | |
501 | 508 | | |
| |||
506 | 513 | | |
507 | 514 | | |
508 | 515 | | |
509 | | - | |
| 516 | + | |
510 | 517 | | |
| 518 | + | |
511 | 519 | | |
512 | 520 | | |
513 | 521 | | |
| |||
524 | 532 | | |
525 | 533 | | |
526 | 534 | | |
| 535 | + | |
527 | 536 | | |
528 | 537 | | |
529 | | - | |
| 538 | + | |
530 | 539 | | |
531 | 540 | | |
532 | 541 | | |
| 542 | + | |
| 543 | + | |
533 | 544 | | |
534 | 545 | | |
535 | 546 | | |
| |||
540 | 551 | | |
541 | 552 | | |
542 | 553 | | |
543 | | - | |
| 554 | + | |
544 | 555 | | |
| 556 | + | |
545 | 557 | | |
546 | 558 | | |
547 | 559 | | |
| |||
678 | 690 | | |
679 | 691 | | |
680 | 692 | | |
681 | | - | |
| 693 | + | |
682 | 694 | | |
683 | 695 | | |
684 | 696 | | |
| |||
699 | 711 | | |
700 | 712 | | |
701 | 713 | | |
702 | | - | |
| 714 | + | |
703 | 715 | | |
704 | 716 | | |
705 | 717 | | |
| |||
719 | 731 | | |
720 | 732 | | |
721 | 733 | | |
722 | | - | |
| 734 | + | |
723 | 735 | | |
724 | 736 | | |
725 | 737 | | |
| |||
738 | 750 | | |
739 | 751 | | |
740 | 752 | | |
741 | | - | |
| 753 | + | |
742 | 754 | | |
743 | 755 | | |
744 | 756 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | 5 | | |
15 | 6 | | |
16 | 7 | | |
| |||
24 | 15 | | |
25 | 16 | | |
26 | 17 | | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
27 | 27 | | |
28 | 28 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| |||
40 | 41 | | |
41 | 42 | | |
42 | 43 | | |
| 44 | + | |
| 45 | + | |
43 | 46 | | |
44 | 47 | | |
45 | 48 | | |
| |||
227 | 230 | | |
228 | 231 | | |
229 | 232 | | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
230 | 237 | | |
231 | 238 | | |
232 | 239 | | |
| |||
510 | 517 | | |
511 | 518 | | |
512 | 519 | | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
513 | 523 | | |
514 | 524 | | |
515 | 525 | | |
| |||
1115 | 1125 | | |
1116 | 1126 | | |
1117 | 1127 | | |
| 1128 | + | |
| 1129 | + | |
| 1130 | + | |
| 1131 | + | |
| 1132 | + | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
| 1144 | + | |
| 1145 | + | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
1118 | 1153 | | |
1119 | 1154 | | |
1120 | 1155 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
0 commit comments