同步数据到Cloud Firestore时关闭应用程序时出现错误。

huangapple go评论75阅读模式
英文:

Error when syncing data to Cloud firestore when they close the app

问题

我在使用React Native Expo和Firestore进行开发,一切似乎都正常运行,但我注意到了一些奇怪的情况,问题是这样的,根据文档,它告诉我:

在Android和Apple平台上,默认情况下启用了离线持久性。要禁用持久性,请将PersistenceEnabled选项设置为false。

当我在应用程序打开时进行离线更改并重新连接到互联网时,数据会完美同步。

问题是当我进行离线更改并关闭应用程序时,当应用程序完全关闭时,我连接到互联网并进入应用程序,先前在离线状态下创建的数据应该在Firestore中得到更新,但是Firestore没有这样做,就好像我从未写入过。

这是我的Firebase设置:

version: "^9.18.0"

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

const firebaseConfig = {
  apiKey: "我的API密钥",
  authDomain: "我的域名",
  projectId: "我的项目ID",
  storageBucket: "我的存储桶",
  messagingSenderId: "我的消息发送者ID",
  appId: "我的应用ID",
};

const app = initializeApp(firebaseConfig);
const database = getFirestore(app);

export { database };

这是数据写入:

import { arrayUnion, arrayRemove, doc, updateDoc } from "firebase/firestore";
import { database } from "../database/firebase";

const onSubmitEdit = async (data) => {
  const userRef = doc(database, "users", email);
  await updateDoc(userRef, { groups: arrayUnion(data) });
  await updateDoc(userRef, { groups: arrayRemove(route.params.item) });
};

const onSubmitCreate = async (data) => {
  const userRef = doc(database, "users", email);

  await updateDoc(userRef, { groups: arrayUnion(data) });
};

这是我的onSnapshot:

import { doc, onSnapshot } from "firebase/firestore";
import { database } from "../database/firebase";

const session = useSelector((state) => state.session);
const activeGroup = useSelector((state) => state.activeGroup);

useEffect(() => {
  if (session) {
    const snap = onSnapshot(
      doc(database, "users", email),
      { includeMetadataChanges: true },
      async (doc) => {
        /* 任务 */
      }
    );
    return () => {
      snap();
    };
  }
}, [activeGroup.active, session]);

我尝试过删除includeMetadataChanges,甚至将onSnapshot放置在没有useEffect限制的情况下,但都不起作用,我更改了配置并尝试了一些来自Firebase的代码,但也没有用,什么都没用。

英文:

I'm working with react-native expo and firestore, everything seemed to be working fine, when I notice something strange, the problem is this, according to the documentation it tells me this.

> On Android and Apple platforms, offline persistence is enabled by default. To disable persistence, set the PersistenceEnabled option to false.

When I make an offline change while the app is open and reconnect to the internet, the data syncs perfectly.

The problem is when I make an offline change and close the app, while the app is fully closed, I connect to the internet and go into the app, the data created earlier while I was offline is supposed to be updated in firestore, but firestore isn't doing it, it's like I never wrote it.

this is my firebase setup
version: "^9.18.0"

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

const firebaseConfig = {
  apiKey: "MY.APIKEY",
  authDomain: "MY.DOMAIN",
  projectId: "MY.PROJECT",
  storageBucket: "MY.STORAGE",
  messagingSenderId: "MY.MESSAGIN",
  appId: "MY.APPID",
};

const app = initializeApp(firebaseConfig);
const database = getFirestore(app);

export { database };

This is data write

import { arrayUnion, arrayRemove, doc, updateDoc } from "firebase/firestore";
import { database } from "../database/firebase";

const onSubmitEdit = async (data) => {
  const userRef = doc(database, "users", email);
  await updateDoc(userRef, { groups: arrayUnion(data) });
  await updateDoc(userRef, { groups: arrayRemove(route.params.item) });
};

const onSubmitCreate = async (data) => {
  const userRef = doc(database, "users", email);

  await updateDoc(userRef, { groups: arrayUnion(data) });
};

This is my onSnapshot

import { doc, onSnapshot } from "firebase/firestore";
import { database } from "../database/firebase";

const session = useSelector((state) => state.session);
const activeGroup = useSelector((state) => state.activeGroup);

useEffect(() => {
  if (session) {
    const snap = onSnapshot(
      doc(database, "users", email),
      { includeMetadataChanges: true },
      async (doc) => {
        /* TASKS */
      }
    );
    return () => {
      snap();
    };
  }
}, [activeGroup.active, session]);

I tried to remove the includeMetadataChanges even placing the onSnapshot without the limitations of the useEffect, but nothing works, I changed the configuration and tried a few of codes from firebase and it doesn't work either, nothing works.

答案1

得分: 1

以下是翻译好的部分:

"The documentation on offline storage being enabled by default applies to the native SDKs for iOS and Android. It does not apply to the JavaScript SDK, which you seem to be using in Expo. So there you will have to enable caching explicitly as shown in the documentation too.

If you get an error message there, it may be that caching isn't possible on Expo. In that case consider exiting Expo and using the react-native-firebase wrappers for the native SDKs."

英文:

The documentation on offline storage being enabled by default applies to the native SDKs for iOS and Android. It does not apply to the JavaScript SDK, which you seem to be using in Expo. So there you will have to enable caching explicitly as shown in the documentation too.

If you get an error message there, it may be that caching isn't possible on Expo. In that case consider exiting Expo and using the react-native-firebase wrappers for the native SDKs.

huangapple
  • 本文由 发表于 2023年3月23日 09:07:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818481.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定