Repository: firebase/firebase-ios-sdk (language: cpp)

Issue:
[FirebaseDatabase] Databases are disconnected temporarily on app goes inactive state.
### Description

Since v10.27.0, `FirebaseDatabase` now temporarily goes offline when the app is in the inactive state (e.g., when control center is opened, when returning to the home screen, etc.) and comes back online after a while.

In v10.26.0, the behavior was below:
<video width=300, src="https://github.com/user-attachments/assets/5008221a-169a-4c17-9266-84e0901ab503" />

but is in v10.27.0 or later:
<video width=300, src="https://github.com/user-attachments/assets/247d3c43-f295-4e9c-96c9-a3d8b9bf9a1a" />


### Reproducing the issue

1. Create iOS App project
2. Integrate Firebase project and add `FirebaseDatabase` package
3. Replace App code with like this:

```swift
import SwiftUI
import UIKit
import FirebaseCore
import FirebaseDatabase

@main
struct RtdbReproIOSApp: App {
    
    @UIApplicationDelegateAdaptor (AppDelegate.self) var appDelegate
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        FirebaseApp.configure()
        
        let connectedRef = Database.database().reference(withPath: ".info/connected")
        connectedRef.observe(.value, with: { snapshot in
          if snapshot.value as? Bool ?? false {
            print("Connected")
          } else {
            print("Not connected")
          }
        })
        
        return true
    }
}

```

### Firebase SDK Version

10.27.0

### Xcode Version

15.3

### Installation Method

Swift Package Manager

### Firebase Product(s)

Database

### Targeted Platforms

iOS

### Relevant Log Output

_No response_

### If using Swift Package Manager, the project's Package.resolved

<!--- Look below for instructions on how to share your Package.resolved. --->

<details>
<summary>Expand <code>Package.resolved</code> snippet</summary>
<br>

```json
{
  "originHash" : "c63c63846d9c539229e96de38d6af51417e28c0ee9a0bc48bd0f0f19d923c329",
  "pins" : [
    {
      "identity" : "abseil-cpp-binary",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/google/abseil-cpp-binary.git",
      "state" : {
        "revision" : "194a6706acbd25e4ef639bcaddea16e8758a3e27",
        "version" : "1.2024011602.0"
      }
    },
    {
      "identity" : "app-check",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/google/app-check.git",
      "state" : {
        "revision" : "3b62f154d00019ae29a71e9738800bb6f18b236d",
        "version" : "10.19.2"
      }
    },
    {
      "identity" : "firebase-ios-sdk",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/firebase/firebase-ios-sdk",
      "state" : {
        "revision" : "8bcaf973b1d84e119b7c7c119abad72ed460979f",
        "version" : "10.27.0"
      }
    },
    {
      "identity" : "googleappmeasurement",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/google/GoogleAppMeasurement.git",
      "state" : {
        "revision" : "70df02431e216bed98dd461e0c4665889245ba70",
        "version" : "10.27.0"
      }
    },
    {
      "identity" : "googledatatransport",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/google/GoogleDataTransport.git",
      "state" : {
        "revision" : "a637d318ae7ae246b02d7305121275bc75ed5565",
        "version" : "9.4.0"
      }
    },
    {
      "identity" : "googleutilities",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/google/GoogleUtilities.git",
      "state" : {
        "revision" : "57a1d307f42df690fdef2637f3e5b776da02aad6",
        "version" : "7.13.3"
      }
    },
    {
      "identity" : "grpc-binary",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/google/grpc-binary.git",
      "state" : {
        "revision" : "e9fad491d0673bdda7063a0341fb6b47a30c5359",
        "version" : "1.62.2"
      }
    },
    {
      "identity" : "gtm-session-fetcher",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/google/gtm-session-fetcher.git",
      "state" : {
        "revision" : "a2ab612cb980066ee56d90d60d8462992c07f24b",
        "version" : "3.5.0"
      }
    },
    {
      "identity" : "interop-ios-for-google-sdks",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/google/interop-ios-for-google-sdks.git",
      "state" : {
        "revision" : "2d12673670417654f08f5f90fdd62926dc3a2648",
        "version" : "100.0.0"
      }
    },
    {
      "identity" : "leveldb",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/firebase/leveldb.git",
      "state" : {
        "revision" : "a0bc79961d7be727d258d33d5a6b2f1023270ba1",
        "version" : "1.22.5"
      }
    },
    {
      "identity" : "nanopb",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/firebase/nanopb.git",
      "state" : {
        "revision" : "b7e1104502eca3a213b46303391ca4d3bc8ddec1",
        "version" : "2.30910.0"
      }
    },
    {
      "identity" : "promises",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/google/promises.git",
      "state" : {
        "revision" : "540318ecedd63d883069ae7f1ed811a2df00b6ac",
        "version" : "2.4.0"
      }
    },
    {
      "identity" : "swift-protobuf",
      "kind" : "remoteSourceControl",
      "location" : "https://github.com/apple/swift-protobuf.git",
      "state" : {
        "revision" : "e17d61f26df0f0e06f58f6977ba05a097a720106",
        "version" : "1.27.1"
      }
    }
  ],
  "version" : 3
}
```

</details>


### If using CocoaPods, the project's Podfile.lock

<!--- Look below for instructions on how to share your Podfile.lock. --->

<details>
<summary>Expand <code>Podfile.lock</code> snippet</summary>
<br>

```yml

Replace this line with the contents of your Podfile.lock!

```

</details>



Discussion / hints:
Thanks for reporting this, @Kurogoma4D. I was able to reproduce the issue. I'll raise this to our engineers and see what we can do here.

Fix patch:
diff --git a/FirebaseDatabase/CHANGELOG.md b/FirebaseDatabase/CHANGELOG.md
index 7ee5692d4f0..7df20ba497e 100644
--- a/FirebaseDatabase/CHANGELOG.md
+++ b/FirebaseDatabase/CHANGELOG.md
@@ -1,3 +1,7 @@
+# Unreleased
+- [fixed] Fix temporary disconnects when the app goes inactive. The issue was
+  introduced in 10.27.0. (#13529)
+
 # 11.0.0
 - [removed] **Breaking change**: The deprecated `FirebaseDatabaseSwift`
   module has been removed. See
diff --git a/FirebaseDatabase/Sources/Core/FPersistentConnection.m b/FirebaseDatabase/Sources/Core/FPersistentConnection.m
index 8c22814df00..f07ee977246 100644
--- a/FirebaseDatabase/Sources/Core/FPersistentConnection.m
+++ b/FirebaseDatabase/Sources/Core/FPersistentConnection.m
@@ -34,11 +34,9 @@
 #import "FirebaseDatabase/Sources/Utilities/FUtilities.h"
 #import "FirebaseDatabase/Sources/Utilities/Tuples/FTupleCallbackStatus.h"
 #import "FirebaseDatabase/Sources/Utilities/Tuples/FTupleOnDisconnect.h"
-#if TARGET_OS_WATCH
-#import <WatchKit/WatchKit.h>
-#else
+#if !TARGET_OS_WATCH
 #import <SystemConfiguration/SystemConfiguration.h>
-#endif // TARGET_OS_WATCH
+#endif // !TARGET_OS_WATCH
 #import <dlfcn.h>
 #import <netinet/in.h>
 
@@ -168,7 +166,6 @@ - (id)initWithRepoInfo:(FRepoInfo *)repoInfo
                         retryExponent:kPersistentConnReconnectMultiplier
                          jitterFactor:0.7];
 
-        [self setupNotifications];
         // Make sure we don't actually connect until open is called
         [self interruptForReason:kFInterruptReasonWaitingForOpen];
     }
@@ -553,29 +550,6 @@ - (void)openNetworkConnectionWithContext:
     [self.realtime open];
 }
 
-- (void)enteringForeground {
-    dispatch_async(self.dispatchQueue, ^{
-      // Reset reconnect delay
-      [self.retryHelper signalSuccess];
-      if (self->connectionState == ConnectionStateDisconnected) {
-          [self tryScheduleReconnect];
-      }
-    });
-}
-
-- (void)setupNotifications {
-#if TARGET_OS_WATCH
-    __weak FPersistentConnection *weakSelf = self;
-    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
-    [center addObserverForName:WKApplicationWillEnterForegroundNotification
-                        object:nil
-                         queue:nil
-                    usingBlock:^(NSNotification *_Nonnull note) {
-                      [weakSelf enteringForeground];
-                    }];
-#endif // TARGET_OS_WATCH
-}
-
 - (void)sendAuthAndRestoreStateAfterComplete:(BOOL)restoreStateAfterComplete {
     NSAssert([self connected], @"Must be connected to send auth");
     NSAssert(self.authToken != nil,
diff --git a/FirebaseDatabase/Sources/Realtime/FWebSocketConnection.m b/FirebaseDatabase/Sources/Realtime/FWebSocketConnection.m
index 6aa9d7365e5..078fbb06ef6 100644
--- a/FirebaseDatabase/Sources/Realtime/FWebSocketConnection.m
+++ b/FirebaseDatabase/Sources/Realtime/FWebSocketConnection.m
@@ -110,27 +110,6 @@ - (instancetype)initWith:(FRepoInfo *)repoInfo
             NSURLSessionWebSocketTask *task =
                 [session webSocketTaskWithRequest:req];
             self.webSocketTask = task;
-
-#if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_VISION || TARGET_OS_MACCATALYST
-            NSString *resignName = UIApplicationWillResignActiveNotification;
-#elif TARGET_OS_OSX
-            NSString *resignName = NSApplicationWillResignActiveNotification;
-#elif TARGET_OS_WATCH
-            NSString *resignName = WKApplicationWillResignActiveNotification;
-#elif
-#error("missing platform")
-#endif
-            [[NSNotificationCenter defaultCenter]
-                addObserverForName:resignName
-                            object:nil
-                             queue:opQueue
-                        usingBlock:^(NSNotification *_Nonnull note) {
-                          FFLog(@"I-RDB083015",
-                                @"Received notification that application "
-                                @"will resign, "
-                                @"closing web socket.");
-                          [self onClosed];
-                        }];
         }
     }
     return self;


Test patch: