0

From the documentation on firestore batch writes and transactions, what I understand, and please correct me if I am mistaken, that transactions atomic document writes after reads, and so that all read operations have to be made before the writes inside of a transaction.

But my question can the writes be made to the realtime database instead of firestore?

For example taking the snippet inside the documentation, will replacing the firestore update(write) to the city document population counter, with a firebase set(write) to a /populations/counter work ?

// Initialize document
var cityRef = firestore.collection('cities').doc('SF');
var setCity = cityRef.set({
  name: 'San Francisco',
  state: 'CA',
  country: 'USA',
  capital: false,
  population: 860000
});

var transaction = firestore.runTransaction(t => {
  return t.get(cityRef)
    .then(doc => {
      // Add one person to the city population
      var newPopulation = doc.data().population + 1;


      // ***** DOCUMENTATION WRITE OPERATION*****
      // t.update(cityRef, {population: newPopulation});



      // ***** CODE IN QUESTION *****
      realtimeDatabase().ref('/counters/population').set(newPopulation)

    });
}).then(result => {
  console.log('Transaction success!');
}).catch(err => {
  console.log('Transaction failure:', err);
});
SaMiCoOo
  • 327
  • 2
  • 10

1 Answers1

1

When working with data in a multi user environment, the available data can be corrupted by concurrent modifications, such as incremental counters. In this case, to have consistent data, you should use a transaction operation. In Cloud Firestore, transactions are useful when you want to update a field's value based on its current value. You could increment a counter by creating a transaction that reads the current value of the counter, increments it, and writes back the new value to Cloud Firestore.

If you read the value of a property within a document and you don't update it back in the transaction operation, it means that is not a transaction operation anymore. If you read that value and instead of updating it, you simply set in another place, it means that you perform a read and a write operation and not an update, where a transaction operation is supposed to be used. So it doesn't matter if you write the new value in the Firebase realtime database or even in another place in Cloud Firestore, this operation cannot be called a transaction.

Also remember, each product has it own transaction mechanism. Here are Firebase realtime database transactions and here are Cloud Firestore transactions.

But my question can the writes be made to the realtime database instead of firestore?

Yes it can, but is not a transaction operation anymore.

Will replacing the firestore update(write) to the city document population counter, with a firebase set(write) to a /populations/counter work?

Yes it will work, but it will be considered only a read/write operation.

If you just want to read a number and set it in the other place, there is no need for a trasanction.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • From my understanding, it doesn't seem that there is any in-built mechanism to perform what could strictly be considered a transaction between a Firestore Collection and a Realtime Database Instance. The best you can do are read-write operations that will not account for data integrity or validity between reading from the source and writing to the destination. – Karan Modi Nov 03 '18 at 07:05