From 0feb9d4ac4ed6b0022a203c68c01693d1912e90f Mon Sep 17 00:00:00 2001 From: Sunghyun Cho Date: Thu, 29 Oct 2026 00:42:17 +0900 Subject: [PATCH] fix(mobile): leaving the iOS app for a few seconds no longer drops the connection React Native holds no background task, so iOS suspends the app about a second after it leaves the foreground and defuncts its sockets. On return the connection supervisor finds the WebSocket silent and replaces it, so even a quick trip to the home screen shows a reconnect, most visibly on T3 Connect routes. A new app-delegate subscriber begins a UIKit background task when the app resigns active, and ends it when the app becomes active again or when iOS expires it. iOS then keeps the app running for up to about 30 seconds, so the socket, its pings and in-flight streams survive short absences. Longer absences behave as before: the app is suspended and the supervisor reconnects on return. Android is unchanged, since its cached-apps freezer waits about 10 seconds before freezing a backgrounded app. --- .../expo-module.config.json | 3 ++- .../ios/T3BackgroundGraceSubscriber.swift | 23 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 apps/mobile/modules/t3-native-controls/ios/T3BackgroundGraceSubscriber.swift diff --git a/apps/mobile/modules/t3-native-controls/expo-module.config.json b/apps/mobile/modules/t3-native-controls/expo-module.config.json index 22fcb74a36cf..5ff3009ef4d2 100644 --- a/apps/mobile/modules/t3-native-controls/expo-module.config.json +++ b/apps/mobile/modules/t3-native-controls/expo-module.config.json @@ -4,7 +4,8 @@ "modules": ["T3NativeControlsModule", "T3KeyboardCommandsModule"], "appDelegateSubscribers": [ "T3WorkspaceOrientationSubscriber", - "T3MenuDismissTouchGateSubscriber" + "T3MenuDismissTouchGateSubscriber", + "T3BackgroundGraceSubscriber" ] }, "android": { diff --git a/apps/mobile/modules/t3-native-controls/ios/T3BackgroundGraceSubscriber.swift b/apps/mobile/modules/t3-native-controls/ios/T3BackgroundGraceSubscriber.swift new file mode 100644 index 000000000000..62eb5d86ad12 --- /dev/null +++ b/apps/mobile/modules/t3-native-controls/ios/T3BackgroundGraceSubscriber.swift @@ -0,0 +1,23 @@ +import ExpoModulesCore +import UIKit + +public final class T3BackgroundGraceSubscriber: ExpoAppDelegateSubscriber { + private var task = UIBackgroundTaskIdentifier.invalid + + public func applicationWillResignActive(_ application: UIApplication) { + guard task == .invalid else { return } + task = application.beginBackgroundTask(withName: "T3 connection grace") { [weak self] in + self?.endTask(application) + } + } + + public func applicationDidBecomeActive(_ application: UIApplication) { + endTask(application) + } + + private func endTask(_ application: UIApplication) { + guard task != .invalid else { return } + application.endBackgroundTask(task) + task = .invalid + } +}