Aryan Samthaan
Aryan Samthaan

Reputation: 45

How delete the oldest child node after entering a new child node in Android

enter image description here

After adding 10 child I want to delete the first child whenever a new child added in the database.

Upvotes: 0

Views: 46

Answers (2)

The best way to do this is to use the firebase cloud functions. Cloud functions is like are triggers ready to perform a certain task, such as when an item is created, added, deleted or updated, and much more.

Read about it on: https://firebase.google.com/docs/functions/get-started

Upvotes: 0

Priyanka
Priyanka

Reputation: 3699

this may help you

rootref.child("messeges").child(YourInnerChildKey).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
      int count = 0;
      String firstNodeId;
      for (DataSnapshot ds : dataSnapshot.getChildren()) {
        if (count == 0) firstNodeId = ds.getKey();
        count++;
      }

      if(count > 10){
         rootref.child("messeges").child(YourInnerChildKey).removeEventListener(this);
         firebase.child(firstNodeId).removeValue();
         rootref.child("messeges").child(YourInnerChildKey).addValueEventListener(this);
      }
}

Upvotes: 1

Related Questions