iKreateCode
iKreateCode

Reputation: 209

Effeciently update data in firestore flutter and prevent many writes

I would like to prevent as much write data to firestore and make it as efficient. The user can edit their profile that consists of three sections. The way i have it at the moment is that the firestore update is stored in a method which updates all the sections even if only one section is updated.

I would like it so that if the user only edits one section then that is only updated within firestore.

My code

    await firestoreInstance
        .collection("users")
        .document(firebaseUser.uid)
        .updateData({
      "images": _imagesUrl,
      "name": firstNameController.text,
      "biography": biographyController.text,
    });

Upvotes: 0

Views: 574

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

The problem here is that we have no way to know the current value of any of these fields.

If you know the current value, you can compare the current and new values and only send it to the database if they are not the same.

If you don't know the current value, then loading the current value is probably more costly than simply sending all three fields to the database.

The reason for that is that Firebase charges for (in this scenario):

  • document writes - each document you modify incurs a cost, but that cost does not depend on the amount of data you update in that document
  • document reads
  • bandwidth of data read by the client

So Firestore doesn't charge bandwidth for data you send to the database. So while only sending modified fields may save bandwidth, it won't save in Firebase cost on that front, while having to read the document to determine what fields are modified will definitely cost more.

Upvotes: 2

Related Questions