简体   繁体   中英

Does Firebase Firestore keep a local copy of snapshot

I am using firebase firestore for my chat room application.

I have a function to send messages to firebase as

const sendMessageHandler = message => {
  if (message) {
    firestore()
      .collection(`ChatRooms/${roomId}/messages`)
      .doc(moment().format('YYYY-MM-DD-HH-mm-sssss'))
      .set({
        message: Encrypt(message),
        userId: userId,
      });
  }
};

and I am fetching messages into flatlist only from firestore as

// this messages const is getting data only from firestore
const [messages, setMessage] = useState([]);

firestore()
  .collection(`ChatRooms/${roomId}/messages`)
  .onSnapshot(
    querySnapshot => {
      const messages = [];
      querySnapshot.forEach(dataSnapshot => {
        messages.push({
          id: dataSnapshot.id,
          message: dataSnapshot.data().message,
          userId: dataSnapshot.data().userId,
        });
      });
      setMessage(messages.reverse());
      setLoading(false);
    },
    error => {
      console.log('error : ', error);
    },
  );


// And now I am using this messages somewhere in return function of a component as : 

<FlatList
  data={messages}
  renderItem={flatListItemRenderer}
  inverted={true}
/>

Notice I am fetching only from firestore and not locally.

Now I turned off the inte.net and tried sending messages so this happens

The data has not been updated to firestore (of course because of no inte.net), but the flatlist has been updated with the new message !!

The only possibility I can think of is that the setter methods of firestore is storing data in both local and remote database and the getter method is first getting snapshot from local database and then remote database.

So the question is does @react-native-firebase/firestore keep a local snapshot also and updates both local and remote snapshot of data whenever we change something?

Github Link

Edit :
Firestore docs says Firestore provides out of the box support for offline capabilities. When reading and writing data, Firestore uses a local database which synchronizes automatically with the server.This functionality is enabled by default, however it can be disabled if you need it to be disabled

I tried turning off persistence, but this property is related to storing data offline not the state. ie, now when my app loads it fetch all the data directly from server(previously fetching from storage and server both), but the flatlist still updates with the new message(maybe it is storing some state like useState also???)

The Firestore SDK keeps a local copy of:

  • All data that you have an active listener for.
  • All pending writes.

In addition, if offline persistence is enabled, it also keeps a local copy of data that your code has recently read.


When you make a write operation, the SDK:

  1. Writes your operation to its queue of pending writes.
  2. Fires an event for any local listeners.
  3. Tries to synchronize the pending write with the server.

Steps 1 and 2 always happen, regardless of whether you are online or offline. Only step 3 won't complete immediately when you are offline.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM