You can use the update counter to make write operations work similar to transactions. (I will use some pseudo code below, sorry for that, but I did not want to write out the complete REST API for an example.)
For example, if I have an object like this:
{ total: 100, update_counter: 0 }
And write the rule as follows:
{ ".write": "newData.hasChild('update_counter')", "update_counter": { ".validate": "newData.val() === data.val()+1" } }
Now I can prevent concurrent changes by simply passing update_counter with each operation. For instance:
var url = 'https://<INSTANCE>.firebaseio.com/path/to/data.json'; addToTotal(url, 25, function(data) { console.log('new total is '+data.total); }); function addToTotal(url, amount, next) { getCurrentValue(url, function(in) { var data = { total: in.total+amount, update_counter: in.update_counter+1 }; setCurrentValue(ref, data, next, addToTotal.bind(null, ref, amount, next)); }); } function getCurrentValue(url, next) {
source share