简体   繁体   中英

Firebase Firestore takes long to reconnect after connection is lost

I'm building an Android application using Cloud Firestore. Specifically I use live subscription (Snapshot Listener). And noticed a strange behavior.

When internet connection is lost, eventually, I cannot get any data from Firestore (considering it's offline cache is empty). When connection is back, I'm still not getting any data for 30-50 seconds. And only after that, quite long period of time, Firestore serves the data again.

Any ideas where this latency comes from and how to deal with it? It's really annoying.

While the device is offline and if you have enabled offline persistence (which in Firestore is enabled by default), your listeners will receive listen events when the locally cached data changes. The first time you attach a listener, Firestore will access the network to download all of the results of your query and provide you a QuerySnapshot object. If you attach the same listener, the second time and you're using offline persistence, the listener will be fired immediately with the results from the cache . After you get the cached result, Firestore will check with the server to see if there are any changes to your query result. If yes you will get another snapshot with the changes.

Note, if you are using a get() call, Firestore will still try to hit the network first, to give you as up-to-date data as possible. If you use addSnapshotListener() instead, Firestore will call you immediately with the cached data, without waiting for the network. That's why you have that ammount of time between the time that you are getting back online and the actual synchronization.

You can also take a look on my answer for this post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM