无法到达 Cloud Firestore 后端 - React native Firebase v9

Could not reach Cloud Firestore backend - React native Firebase v9

我在 SO 上看到很多关于这个问题的问题,其中 none 个得到了回答(或者解决方案不起作用),我不知道为什么。人们不断遇到此错误,但没有提供解决方案。从过去几天开始,我什至遇到了这个错误(注意:它似乎在我的物理设备上运行良好(不是在调试时,它只有在我发布它时才能运行),但在 android 模拟器,所以我很确定我的互联网工作正常):

Setting a timer for a long period of time, i.e. multiple minutes, is a performance and correctness issue on Android as it keeps the timer module awake, and timers can only be called when the app is in the foreground. See https://github.com/facebook/react-native/issues/12981 for more info.
(Saw setTimeout with duration 3052257ms)
Authorization status: 1

[2022-02-09T07:05:26.500Z]  @firebase/firestore: Firestore (9.6.5): 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.
Authorization status: 1

[2022-02-09T07:08:44.688Z]  @firebase/firestore: Firestore (9.6.5): Connection WebChannel transport errored: me {
  "defaultPrevented": false,
  "g": Y {
    "A": true,
    "J": null,
    "R": [Circular],
    "g": $d {
      "$": true,
      "$a": 2,
      "A": 3,
      "B": null,
      "Ba": 12,
      "C": 0,
      "D": "gsessionid",
      "Da": false,
      "Ea": Dd {
        "g": Cd {},
      },
      ... & so on...

Package.json:

"@react-native-firebase/app": "^14.3.1",
"@react-native-firebase/messaging": "^14.3.1",
"@react-native-google-signin/google-signin": "^7.0.4",
"expo": "~44.0.0",
"firebase": "^9.6.3",

身份验证和消息传递似乎工作正常,但我认为来自 firestore 的 db 存在此问题。 以下是我的 firebase.tsx 配置文件中的一些代码:

import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";
import { getFirestore } from "firebase/firestore";

const firebaseConfig = {
  apiKey: "",
  authDomain: "",
  databaseURL: "",
  projectId: "",
  storageBucket: "",
  messagingSenderId: "",
  appId: "",
  measurementId: "",
};

const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const db = getFirestore(app);

export { auth, db };

到目前为止我尝试了什么:

  1. 更改此答案中提到的 firestore 数据库规则:

  2. 使用此解决方法:https://github.com/firebase/firebase-js-sdk/issues/5667#issuecomment-952079600

  3. 根据这个答案,我没有使用任何 .env 变量:

  4. 擦除 android 模拟器数据,然后冷启动。没用。

  5. 已将 firebase-9.6.3 升级为 firebase-9.6.6。没用。

  6. 清理了构建文件夹,yarn 再次安装了所有包。没用。

任何人都可以提供解决此问题的有效方法吗? 谢谢!

EDIT-1:

我特别调用 firestore db 的代码:

在我的 HomeScreen.tsx:

import { doc, setDoc, onSnapshot, collection } from "firebase/firestore";
import { db } from "../../../firebase";

// inside functional component
useEffect(() => {
    const addUser = () => {
      const path = doc(db, "Users", user.uid);
      const data = {
        _id: user.uid,
        name: user.displayName,
        email: user.email,
      };
      setDoc(path, data)
        .then(() => {
          console.log("User added to db");
        })
        .catch((err) => {
          console.log("Error while adding user to firestore: ", err);
        });
    };
    addUser();
  }, []);

useEffect(() => {
    const unsub = onSnapshot(collection(db, "Items"), (snapshot) =>
      setItems(snapshot.docs.map((doc) => doc.data()))
    );
    return () => unsub();
  }, []);

// so nothing is being displayed here as firestore is not working.

经过大量搜索,我自己找到了解决方案。尽管我不得不从头开始创建一个新的裸反应本机项目,即使在那时我也遇到了那个错误,但那时我真的对 firebase 失去了希望。然后过了一段时间,我将 firebase 配置更改为以下代码并且它起作用了:

import {initializeApp} from 'firebase/app';
import {getAuth} from 'firebase/auth';
import {initializeFirestore} from 'firebase/firestore';

const firebaseConfig = {
  apiKey: '',
  authDomain: '',
  projectId: '',
  storageBucket: '',
  messagingSenderId: '',
  appId: '',
  measurementId: '',
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const db = initializeFirestore(app, {
  experimentalForceLongPolling: true,
});

export {auth, db};

我必须如上所述更改 db 的初始化,希望它对遇到这个奇怪错误的每个人都有效。

谢谢大家的帮助!