Skip to content Skip to sidebar Skip to footer

Firebase Onchildadded Not Called When There Is No Data

I want to check if there is child inside or not.When I have data then it goes inside OnChildAdded else when there is no child that is not being called? I want to know if there is a

Solution 1:

As its name implies onChildAdded() will only be called if a child is added to the collection. If there are no children, it will not be called.

To detect the situation where there are no children, you can attach a value-event listener.

Query query = ref.orderByKey()
                 .startAt(todayDate)
                 .endAt(tomorrowDate)
                 .limitToFirst(1);
query.addValueEventListener(new ValueEventListener() {
  publicvoidonDataChange(DataSnapshot snapshot) {
    if (snapshot.getChildrenCount() == 0) {
      System.out.println("No children");
    };
  }
  publicvoidonCancelled(DatabaseError error) {
    throw error.toException();
  }
});

Post a Comment for "Firebase Onchildadded Not Called When There Is No Data"