I have a little issue with correctly incrementing a value in a Realtime Database. Here is how it looks:
MyList
+ -M93jOoZSx443cxXXDKN
- -M93jF4qK9MheZvAuX6k
count: 0
name: "GHD"
+ -M93jCPkGsW4swOZS0JL
Let us say I need to increment count, on the second item from 0 to 1.
This is very simple, nonetheless due to possible concurrent writings it has to be done right. And it is not easy to find out how to do it. I have read that we should use transaction, but haven't found the proper syntax for that. Beside many document do not state clearly if they apply to Realtime Database or not.
This document has an example that I tried to apply but was unsuccessfull.
I tried something like this (wrong):
var dbRef = firebase.database().ref('MyList/'+item.ref+'count');
dbRef.transaction(function(currentRank) {
// If users/ada/rank has never been set, currentRank will be `null`.
return count + 1;
});
To sum it up, my question is what is the correct way to increment my count?