firebase-ios-sdk: Realtime Database stays offline and doesn't reconnect after sleeping
[REQUIRED] Step 1: Describe your environment
- Xcode version: 13.2.1
- Firebase SDK version: 8.15.0
- Installation method: CocoaPods
- Firebase Component: Database
- Target platform(s): macOS
[REQUIRED] Step 2: Describe the problem
Steps to reproduce: Have the mac going to sleep. After waking up, sometimes (with really low frequency), the Realtime Database client would stay offline indefinitely, even if network conditions are good.
We have an app that uses Realtime Database for implementing Presence in Firestore. https://firebase.google.com/docs/firestore/solutions/presence. If we have the app running and the mac goes to sleep, sometimes, after waking up, the Realtime Database client doesn’t reconnect.
We have strong evidence that database.reference(withPath: ".info/connected").observe reported a false value while the mac was sleeping but it never reported true again, even after waking up and having good network conditions.
If we use any operation while Realtime database is offline, like statusReference.setValue to write to the database, the operation never runs the completion callback.
Firestore is able to reconnect properly without problems but Realtime Database client is not working properly.
We saw one case that recovered by itself after a some hours (12 hours after disconnected, 2 hours after the mac woke up). But as we need the Realtime Database to implement Presence, that delay is not acceptable.
Restarting the app makes it work again.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 19 (4 by maintainers)
Hi, this issue is more widespread than just macos, it definitely happens on iOS as well in ionic cordova apps (environment info below).
A couple observations and research items of note that may help:
It’s quite a bit of effort to setup a new ‘minimal’ repo, ionic, angularfire and a subscription to a listval with detection events on resume/background. If someone else has that handy then go for it, I hope this post helps someone else so they don’t spend days searching and experimenting!
Note: using modular angularfire.
ENVIRONMENT
Hello, I had a similar problem while using Firebase web modular API with Capacitor on iOS. Each time a tried a write operation on the database (with set()), and a network change occurred meanwhile, the database API got stuck forever. My listener to “.info/connected” was never called. I think a found a workaround:
In my case, when the app goes offline then online the listener to “.info/connected” is never called again. This means when the app reconnects to WiFi again, my app is not able to update its online status again in the realtime database.
this cost me a lot of negative reviews on my app, I guess we need to remove rtdb. I tried @markterrill solution listening the lifecycle of the app but it doesn’t work, rtdb never recovers and it doesn’t show an error or something in debug window.
Thanks, it sounds like the right way to fix this is to just retry on socket connection failures, respecting the
NSWorkspacestate. I’ll take a crack at implementing this when I have the bandwidth.