Is a local snapshot stored by Firebase Firestore?

In my chat room application, I am utilizing Firebase Firestore for data storage.

One of the functions in my app handles sending messages to Firebase like this:

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,
      });
  }
};

To display messages in a flatlist, I fetch them exclusively from Firestore as shown below:

// This 'messages' const only retrieves data from Firestore
const [messages, setMessage] = useState([]);

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


// The 'messages' are then used in the component's return function as follows: 

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

Note: I retrieve data solely from Firestore and not locally.

Upon disabling internet connection, attempting to send messages resulted in the flatlist updating with the new message without the data being updated in Firestore. This behavior might be due to Firestore storing data both locally and remotely as suggested by the setter methods updating both databases and the getter methods retrieving from them sequentially.

The question arises whether @react-native-firebase/firestore maintains a local snapshot alongside the remote snapshot and updates both when modifications occur?

GitHub Link

Edit :
Firestore documentation confirms that it supports offline capabilities by default. When reading or writing data, Firestore utilizes a local database that syncs automatically with the server. While turning off persistence affects data storage offline, the flatlist still updates with new messages, hinting at potential state management by Firestore beyond just data.

Answer №1

The Firestore SDK stores a local copy of:

  • All data that is being actively listened for.
  • All pending write operations.

Additionally, if offline persistence is turned on, it also retains a local copy of data that has recently been accessed by your code.


When you perform a write operation, the SDK:

  1. Adds your operation to its queue of pending writes.
  2. Triggers an event for any local listeners.
  3. Attempts to synchronize the pending write with the server.

Steps 1 and 2 always occur, regardless of online or offline status. Only step 3 may be delayed when offline.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Finding the data type based on the button clicked with javascript: A beginner's guide

I am trying to work with a PHP function that generates dynamically created divisions. Each of these divisions contains a different data type and a button. How can I extract the data type of a division when the user clicks on the submit button using JavaScr ...

Issue with Material-UI: Unforeseen TypeError arises while toggling between two Drawer elements

Note: I am utilizing material-ui 3.3.0 My goal is to have two <Drawer> components displayed on a page, with one sliding in from the left and the other from the right. In an <AppBar>, there are two <Button> components that toggle the visi ...

What is causing the Password Verification code to malfunction when used with a jquery form wizard?

I have implemented a jquery form wizard for my multi-page form. You can view an example of the form structure by visiting . Currently, the form utilizes a jquery validation plugin. However, I noticed that the jquery.validation.js file lacks validation rul ...

selenium-webdriver causing issues on a nodejs server

Encountering an error while trying to start the nodejs server with selenium webdriver ubuntu@ip-10-10-10-193:~/testenvoy$ node app.js /home/ubuntu/testenvoy/node_modules/selenium-webdriver/index.js:115 static createSession(...args) {} ...

Looking for a script to automate clicking on a specific web element using JavaScript

When working with Selenium, we utilize an action to interact with an element by clicking on it at specific coordinates (X, Y). action.MoveToElement(element, X, Y).Click().Build().Perform() I am looking to achieve the same functionality using Javascript. ...

issues arise post-transpilation with creating errors

In order to practice, I decided to create a basic TypeScript project. If it could be helpful, here is my ts.config: { "compilerOptions": { "target": "es2016", "module": "commonjs", "outDir": "./dist", "esModuleInterop": true, "forceC ...

Incorporating Bootstrap JS into Next.js

Currently, I am in the process of learning next.js and experimenting with incorporating Bootstrap into my projects. To begin, I initiated a new project using npx create-next-app@latest my-app, utilizing the newly created "app" directory structure. Follow ...

The 'this' variable is malfunctioning when trying to assign a JSONP array to an AngularJS controller

I'm working with a REST service in my angular controller and using JSONP to make the call. I need to store the array that is returned from the service into a variable. This is what I currently have: this.testTheRest = function() { $http.jsonp(&a ...

Angular - Enhance ngFor index while filtering

I am currently working with a list that utilizes an *ngFor loop in the template: <li *ngFor="let product of products | filterProducts: selectedFilter; index as productId"> <a [routerLink]="['/product', productId]"> {{produc ...

Excessive requests to location or history APIs in a brief period of time

Alert: Reached maximum update depth. This issue may arise when a component invokes setState within useEffect without a dependency array, or if any of the dependencies change on each render. const OwnerPage = () => { const onOpen = useAgencyModal((s ...

Why is the server displaying a blank white page following the import of Material UI icons/module?

Here is the code snippet I am working on: import React from 'react' import "./Chat.css" import { Avatar, IconButton } from "@material-ui/core"; import SearchOutlinedIcon from '@mui/icons-material/SearchOutlined'; imp ...

Validating checkboxes using HTML5

When it comes to HTML5 form validation, there are some limitations. For instance, if you have multiple groups of checkboxes and at least one checkbox in each group needs to be checked, the built-in validation may fall short. This is where I encountered an ...

Having difficulty accessing the Material UI Icons

I encountered an issue when attempting to utilize Material UI icons - as soon as I added the icon component, an error occurred. https://i.stack.imgur.com/issmm.png For reference, you can find the code on CodeSandbox at the following link: https://codesand ...

Implementing functions within a loop with the help of requirejs

I'm experiencing a challenge with calling functions within a loop across different modules using requirejs. The function call within the loop is located in module A and triggers a function in module B that initiates an Ajax request using jQuery. Each ...

Loading React.js components before data fetch is complete

My app is encountering an issue where it renders before the fetch operation is completed, resulting in incorrect rendering. Below is the code snippet: componentWillMount() { fetch('http://localhost:8081/milltime/login?users='+this.state.e ...

AJAX: Send a value, perform a query based on this value, and retrieve a JSON array

Can anyone explain why this code isn't functioning properly? I am attempting to submit a value from a form to a PHP file that performs a select query in my database using that value. The goal is to return a JSON array to Ajax and display all the resul ...

Keying objects based on the values of an array

Given the array: const arr = ['foo', 'bar', 'bax']; I am looking to create an object using the array entries: const obj = { foo: true, bar: true, bax: false, fax: true, // TypeScript should display an error here becau ...

AngularJS is failing to recognize the onload event when loading a URL

I am currently working with the AngularJS Framework and I have encountered an issue. My directive only seems to work when the window is fully loaded. screensCreators.directive('createscreen', function () { return { restrict: "A", ...

Objects shifting position as the browser window is resized or zoomed

I've scoured through numerous examples, but none seem to work for my specific situation. On my webpage, I have multiple tables nested within a <div>, which serves as a background element (as it doesn't cover the entire screen). However, whe ...

Adjusting the image placement within a modal window (using bootstrap 3)

Behold, an example modal: <!-- Large Modal --> <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel"> <div class="modal-dialog modal-lg"> <div class="modal-content"> ...