Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore fails to retrieve data after reconnecting to the Internet

Tags:

I'm using Firestore with the Android SDK (11.6.2) and I'm hitting an exception when my device was offline and reconnects to the Internet.

When requesting a document, firestore fails with the following task exception :

com.google.firebase.firestore.FirebaseFirestoreException: Failed to get document because the client is offline.

However, the device is connected, I can make network requests beside using Firestore and they succeed. This error is not consistent, sometimes the request will succeed right after reconnecting to the Internet. Sometimes, the request fails again and again, then succeeds, sometimes more than one minute after the device has been reconnected to the Internet.

Here is a sample request that produces the exception:

val docRef = firestore.collection("foo").document("bar")
docRef.get().addOnCompleteListener { task ->
    if (task.isSuccessful) {
        Log.d("FirestoreSourceSet", "Get document success")
    } else {
        Log.e("FirestoreSourceSet", "Get document error", task.exception)
    }
}

I'm not using the offline capabilities of Firestore, thus the FirebaseFirestore instance is initialized the first time with the setPersistenceEnabled(false) flag:

val firestoreSettings = FirebaseFirestoreSettings.Builder()
    .setPersistenceEnabled(false)
    .build()

val firestore = FirebaseFirestore.getInstance().apply {
    this.firestoreSettings = firestoreSettings
}

Why is Firestore returning this error even though the device is online? Am I missing something in the Firestore configuration that would avoid this error?

I tried upgrading Firebase to the 11.8.0 version, but I encounter the same behavior.

Logs

These are the logs while trying to fetch sync some data with Firestore (which begins with a document fetch) after leaving airplane mode: https://pastebin.com/xDMG2Pzj

The network is already available before the first Firestore call, as I waited for the Wifi to settle, and check it using the ConnectivityManager of Android before proceeding with Firestore.

The multiple calls are because I manually retry using a button each time I get the error until the document is successfully retrieved.

The first line of the log is when I turn the airplane mode one, which closes the stream of Firestore.

Edit: Issue

Firebase doesn't have a public tracker, but I reported the issue using their report tool, and made a repo that reproduces the issue.

They acknowledged the issue but could not provide an ETA, so we have to wait:

If we release the fix, we will announce it in our release notes page.

This is still an issue as of firestore-core 17.0.4

like image 482
Matthieu Esnault Avatar asked Jan 10 '18 13:01

Matthieu Esnault


People also ask

Can firestore work offline?

Note: Offline persistence is supported only in Android, Apple, and web apps. To use offline persistence, you don't need to make any changes to the code that you use to access Firestore data.

Is firestore too expensive?

Although Firestore is very affordable with little usage, costs start to increase as you begin to scale. In most cases, the cost of using Firestore at scale is more expensive than other providers.

How do I restore my firestore collection?

There is no way to restore neither a document nor a collection at the moment. If you are storing potential data, which you can't afford to lose, A good practice would be to create another project under the same account and frequently export the entire database to that project.


1 Answers

From the official documentation:

To use offline persistence, you don't need to make any changes to the code that you use to access Cloud Firestore data. With offline persistence enabled, the Cloud Firestore client library automatically manages online and offline data access and synchronizes local data when the device is back online.

When you initialize Cloud Firestore, you can enable or disable offline persistence. So, when using the following line of code:

val firestoreSettings = FirebaseFirestoreSettings.Builder()
    .setPersistenceEnabled(false)
    .build()

You actually set the persistence to false, you are disabling this feature. To solve this, just remove this line of code. Firestore has .setPersistenceEnabled(true) by default.

like image 78
Alex Mamo Avatar answered Oct 31 '22 15:10

Alex Mamo