11

For incrementing an int value in my database , I first get that value by using a listener , increment it by 1 and then set the new value to database. This works but I want to know if there is an easier way of doing this. This way seems like too much work.

adjuremods
  • 2,870
  • 2
  • 11
  • 16

2 Answers2

20

Update: since early 2020 there actually is a server-side operation to increment values in the Realtime Database. See dfeverx's answer on this, or my own Q&A comparing performance between transactions and increments: How quickly can you atomically increment a value on the Firebase Realtime Database?.


There is no server-side way to increment values (or do other calculations) in the Firebase Database.

Your approach is one way of doing this, but it has the chance of leading to a race condition.

  1. the value in the database is 12
  2. client 1 reads the value 12
  3. client 2 read the value 12
  4. client 1 writes its incremented value 13
  5. client 2 writes its incremented value 13

The result is now likely incorrect (it depends on your use-case).

In that case, one way to make it work is to use Firebase transactions, which combine the reading and writing into a single function.

Transactions are covered in the Firebase Database documentation. I highly recommend reading it. A few hours spent there will prevent many problems down the road. From the docs:

postRef.runTransaction(new Transaction.Handler() {
    @Override
    public Transaction.Result doTransaction(MutableData mutableData) {
        Long value = mutableData.getValue(Long.class);
        if (value == null) {
            mutableData.setValue(0);
        }
        else {
            mutableData.setValue(value + 1);
        }

        return Transaction.success(mutableData);
    }

    @Override
    public void onComplete(DatabaseError databaseError, boolean b,
                           DataSnapshot dataSnapshot) {
        Log.d(TAG, "transaction:onComplete:" + databaseError);
    }
});
Frank van Puffelen
  • 499,950
  • 69
  • 739
  • 734
16

From firebase JavaScript SDK v7.14.0 you can use ServerValue.increment() to increment value in firebase

Method 1

var userRef= firebase.database().ref("user/user_id_123");
userRef.push({
  likes: firebase.database.ServerValue.increment(1)
});

Method 2

firebase.database()
    .ref('user')
    .child('user_id_123')
    .child('likes')
    .set(firebase.database.ServerValue.increment(1))

You can also pass float or negative value to the increment function

You can find the reference here: https://firebase.google.com/docs/reference/js/v8/firebase.database.ServerValue

ilbonte
  • 142
  • 1
  • 2
  • 9
d-feverx
  • 1,025
  • 2
  • 14
  • 26