Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore stops updating after losing and regaining an internet connection

I have set up some listeners like so:

deviceListener = db.addSnapshotListener(this::handleDbChange)

When I have a stable internet connection, the handler fires on a data change and allows me to update my application. However, when I lose and regain an internet connection the handler ceases to fire. This doesn't always occur on the first loss of connection, but it always occurs after 2 or 3 drops in my connection.

I have tried removing the listeners and re-adding them when the network changes. Additionally, I tried getting the data directly after the network connection is reestablished:

db.get().add().addOnCompleteListener {
   val snapshot = it.result
   snapshot.toObject(Model::class.java)
}

But, this still serves the stale data. The only way I've found to correct this issue is restarting the app.

If anyone else has encountered this issue, I'd appreciate any insight you may have on how to solve it. FYI, I'm using the com.google.firebase:firebase-firestore:17.0.2 version of the library.

like image 668
Jeremiah Zucker Avatar asked Jul 09 '18 08:07

Jeremiah Zucker


People also ask

How many connections can firestore handle?

Keep in mind the standard limits for Firestore. Pay special attention to the 1 write per second limit for documents and the limit of 1,000,000 concurrent connections per database. These are soft limits that Firestore does not stop you from exceeding.

How do I check my firebase connection?

You can now open and use the Assistant window in Android Studio by following these steps: Click Tools > Firebase to open the Assistant window. Click to expand one of the listed features (for example, Analytics), then click the Get Started tutorial to connect to Firebase and add the necessary code to your app.

How do I update my firestore record?

Firestore Update Entire DocumentgetDatabase() → where we want to update a document. doc() → where we'll be passing references of database, collection and ID of a document that we want to update. setDoc() → where we actually pass new data that we want to replace along with the doc() method.


1 Answers

I know its a late reply, and i'm only a novice here (so I could be wrong), but for anyone else to come across this... it may be a combination of the problem I had:

Firebase Firestore batch command wont commit after regaining connection

And the problem someone else had:

Firestore doesn't immediately start listening to changes when Internet Connection Resumes


In summary:
Ensure you test without an emulator.
If you need live data, turn data persistence off.
And Firestore may use an uncontrollable timer to dictate when it reconnects its listeners after a connection is regained.

like image 180
NicCoe Avatar answered Oct 24 '22 09:10

NicCoe