使用 Firestore 实时快照侦听器,arrayUnion 和 arrayRemove 是否会触发从服务器重新加载整个阵列?

With Firestore real time snapshot listeners, does arrayUnion and arrayRemove trigger a reload of the full array from the server?

使用 Firestore、React Native、RN Firebase、TypeScript。

我正在考虑使用数组存储书签的优缺点post 来自用户的 Uid。

我什至可以用它为用户的书签页面生成内容列表,按最新排序,但我觉得我宁愿将带有标题和 ImageURL 的数据复制到一个单独的 collection 用于特定用途。

它是一个内容应用,有点类似于博客。主要用途是在首页post的列表中,以及post的页面中显示一个可点击的书签图标。我预计书签 collections 最多 3000 个项目,而大多数将在 100 个以下。我们可以想象一个用户在疯狂阅读时可以连续添加多达 10 个书签。

我可能会像这样收听包含数组的用户文档(或者将它分成另一个 collection 以避免膨胀用户文档):

import React, { useEffect } from 'react';
import firestore from '@react-native-firebase/firestore';

function User({ userId }) {
  useEffect(() => {
    const subscriber = firestore()
      .collection('Users')
      .doc(userId)
      .onSnapshot(documentSnapshot => {
        console.log('User data: ', documentSnapshot.data());
      });

    // Stop listening for updates when no longer required
    return () => subscriber();
  }, [userId]);
}

然后我可以点击主页列表或页面中的书签图标。它会触发这样的操作:

// Atomically add a new bookmark to the "bookmarks" array field.
var arrUnion = bookmarks.update({
  bookmarks: admin.firestore.FieldValue.arrayUnion('ItemId_127892')
});
// Atomically remove a bookmark from the "bookmarks" array field.
var arrRm = bookmarks.update({
  bookmarks: admin.firestore.FieldValue.arrayRemove('ItemId_127892')
});

但是,如果用户有一个包含 100 或 1000 个项目的数组,我宁愿避免这种情况,以便他在每次添加书签时都从数据库中重新加载整个数组。这肯定会毁了他的数据计划。

另一点是我需要立即在 UI 上看到更改。所以我也需要在侦听器的缓存中快速更新。

那么,关于与缓存和服务器的数据交换,它具体是如何工作的?

However if the user has an array of 100 or 1000 items, I would prefer to avoid this to him reload the full array from the database each time he add a bookmark. It would certainly kill his data plan.

不一定:如果启用本地缓存(Android 和 iOS 上的默认设置),第二次需要文档时,它实际上将来自本地缓存而不是重新传输相同的来自服务器的数据。