Skip to content Skip to sidebar Skip to footer

Store Int Using Firebase Realtime Database

I've connected my app to Firebase Realtime database and have set up a data class but haven't found a way to save an int to the database. I would like to store the number of times a

Solution 1:

To atomically increment a value in the database, you can do:

Database.database().reference().setValue(["counter": ServerValue.increment(1)]);

If the value already exists, this increments it by 1. If the value doesn't exist yet, this sets it to 1.

Solution 2:

Try below. Basically we are getting the Total node and updating its value.

Step 1:

var database : FirebaseDatabase = FirebaseDatabase.getInstance().getReference("Total")

Step 2:

database.child("value").setValue(incrementNumber);

Post a Comment for "Store Int Using Firebase Realtime Database"