Could not reach Cloud Firestore backend strange behavior

Viewed 1239

I am experiencing some weird behavior when trying to retrieve data from firebase, when I launch the app and run this function:

            const _query = query(_collection, where("userid", "==", uniqueUserID))
            const querySnapshot = await getDocs(_query)
            querySnapshot.forEach((doc) => {
                console.log(doc.data())
            });

I am given this error:

Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds.
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to 
the backend.

However, if I manually refresh the app and the useEffect that contains that function is ran again, it successfuly gets the data. This is pretty strange considering both functions are the exact same and get called whenever the view is loaded or refreshed. How do I fix this?

NOTE: I am using firebase 9.6.11 and expo

EDIT: After plenty of research I finally found something that has seemingly fixed the issue, when running the emulator you need to make sure that the API level is above 25, this fixed it in android, for iOS I can't verify. I don't know why this happens.

2 Answers

Since this question has died down I will close it. Although it wasn't fixed, the problem seemed to be much less ocurrent whenever I was on a real device so in production it wasn't really a problem, meanwhile, I decided to add more catches in order to go around the problem and not have to really deal with it, still annoying tho.

You could specify custom configurations for your Cloud Firestore instance. You can initialize your Firebase app like this:

const app = initializeApp(firebaseConfig);
const db = initializeFirestore(app, {
  experimentalForceLongPolling: true,
});

As you can see on the above code, I used experimentalForceLongPolling. It forces the SDK’s underlying network transport (WebChannel) to use long-polling. Each response from the backend will be closed immediately after the backend sends data (by default responses are kept open in case the backend has more data to send). This avoids incompatibility issues with certain proxies, antivirus software, etc. that incorrectly buffer traffic indefinitely.


For more information, you may checkout this documentation for more Firestore custom configurations.

Related