2
votes

I'm using Firebase in my app. I have a list of element to retrieve using this code:

FirebaseDatabase.getInstance()
.getReferenceFromUrl("myUrl")
.orderByChild("timestamp")
.addChildEventListener(this);

What's the best Approach to know if a ref url has no data inside? Because using ChildEventListener is not possible.

ChildEventListener has only these 4 methods: onChildAdded(), onChildChanged(), onChildRemoved(), onChildMoved().

None of these method is called in case of your object has no item inside.

So my question is. Do I have to use a ValueEventListener as well? I guess onDataChange() is called if no item has been found.

A possible scenario: You have a list of item to retrieve and populate using ChildEventListener. You want to cover the case when no item has been retrieved. So show a text "No data" instead of the list.

3
in the functions try: datasnapshot.getChildrenCount(). if its 0 then there are no items - Janwilx72
yes that's what I thought.. so you have to use both ChildEventListener and ValueEventListener? - br00
dataSnapshot in ChildEventListener is one level deeper than dataSnapshot produced by ValueEventListener, so unfortunately this will not work - koceeng

3 Answers

2
votes

As far as I know, if you request data by addChildEventListener and addValueEventListener on the same data path directly after each other (in the code), you will have result of addChildEventListener completed first, then onDataChange inside ValueEventListener will be executed.

So my solution is doing like this:

Boolean childExist = false;

ref.addChildEventListener(new ChildEventListener() {
    ... onChildAdded() {
        // you can use List or Map here, this Boolean just to indicate if child exist or not
        childExist = true;
    }
    ...
})
ref.addValueEventListener(new ValueEventListener() {
    ... onDataChange() {
        // code you place here will be executed AFTER all of the event inside ChildEventListener is done
        // here the value of childExist will really indicate if there is child or no
    }
    ...
});
1
votes

You can always use just a valueEventListener and onDataChange just check if datasnapshot.exists(). In line with the javadoc here -> https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#exists

so in your case

    FirebaseDatabase.getInstance()
        .getReferenceFromUrl("myUrl")
        .orderByChild("timestamp")
    .addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()){
        //Show text retrieved


         } else {
    //Show "No data"
    }
        }
@Override
public void onCancelled(DatabaseError databaseError) {
 Log.v(FB_ERROR_TAG, "Error in Database Connection");
}
 });
0
votes

If all you want is to show "No data" when there are no results you can just set the text value of your label to "No data" by default and have the firebase query overwrite it. If there are no results it simply won't get overwritten.