Firebase: How to click on a transaction?

Let's say I have the following structure:

{
  "-InSwU2yHOEHwhP_m4_n" : {
    ".priority" : 0.0,
    "name" : "abc"
  },
  "-InSw_P0j8A-0Njj0Uvf" : {
    ".priority" : 1.0,
    "name" : "def"
  }
}

I would like to add another similar element with a directive as a key, and the priority is equal to the number of elements currently represented (in this case 2). The result should look something like this:

{
  "-InSwU2yHOEHwhP_m4_n" : {
    ".priority" : 0.0,
    "name" : "abc"
  },
  "-InSw_P0j8A-0Njj0Uvf" : {
    ".priority" : 1.0,
    "name" : "def"
  },
  "-InSxV-RVkZ07_f3uDnJ" : {
    ".priority" : 2.0,
    "name" : "ghi"
  }
}

Please note: since several clients may try to add such an item at the same time, it must be a transaction.

Any ideas?

+5
source share
1 answer

When used, push()each element is assigned a unique identifier. Thus, a transaction is not needed (there can be no collisions).

, ( , ), , .

, , :

FB.child(path).transaction(function(current_val) {
   if( current_val === null ) {
      /* set the value here */
   }
   /* do nothing; transaction fails because it was already written */
}, function(success) {
   /* transaction done */
});

, , , ; , ; , , .

+3

All Articles