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?
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.