0

I can't get transaction to work and need some help

I created the following db structure using the FB app dashboard: flickering-heat-528 test settings: "hello"

I can see in the dashboard that the data is there:

enter image description here

I then do:

var settingsRef=new Firebase('https://flickering-heat-528.firebaseio.com/test/settings');

settingsRef.transaction(function(json) {
    alert(json);
}, function(error, committed, snapshot) {
}, true);

see jsfiddle

But the alert is always 'null' why ??

kofifus
  • 17,260
  • 17
  • 99
  • 173

1 Answers1

1

Transaction callbacks in Firebase may fire multiple times. To understand why that is, let's see how this operation may be executed.

Let's start with this logic: if the current value is null, you want it to become "byebye".

settingsRef.transaction(function(current) {
    if (!current) {
       return "byebye";
    }
    return current;
}, function(error, committed, snapshot) {
}, true);

The Firebase client doesn't yet know the value of your settings node, so it invokes your transaction with an assumption: that the current value is null. It then sends the combination of the value if gave to you and the value you returned to the Firebase server.

The Firebase server compares the current value in the database ("hello") against the value it provided to you and decides they don't match ("hello" <> null). So it sends the current value ("hello") back to the client.

The Firebase client invokes your callback again. This time it passes in the value it heard from the server ("hello"):

settingsRef.transaction(function(current) {
    if (!current) {
       return "byebye";
    }
    return current + "!";
}, function(error, committed, snapshot) {
}, true);

So now your function returns "hello!". Firebase again sends the value it gave you and the value you returned to the server: "hello" and "hello!". The server does the same comparison as before and if the value in the database is still "hello" it sets the new value you specified "hello!".

This is called a compare-and-set operation and explains why your callback may be called multiple times.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • thx Frank ! so does that mean updateFunc is always first called with null ? if yes isn't that first call useless and can it be voided ? – kofifus Nov 07 '15 at 04:24