Home > Software design >  Firebase realtime DB update child
Firebase realtime DB update child

Time:08-30

I have a "Users" data structure like this, I want to increment the value of ttl_user by 1 when a user is signed up. And decrement it by 1 when a user has deleted their account.

How can I update the value? Thanks.

CodePudding user response:

I'm not able to comment can you please tell me if this "ttl_user" is all user value or a particular one user value if this is All User Value Than You Need To maintain one Variable. That's Help to Archive this issue.

CodePudding user response:

In your particular case, it would be as simple as:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference userRef = db.child("User");
userRef.child("ttl_user").setValue(ServerValue.increment(1));

And to decrement, simply pass a negative value -1.

  • Related