1

From the firebase example, I need to find out the if there are any dinosaurs of age=25, which can be done as below. But say if there are no dinosaurs of that age, how can I find out that the query is finished and there are 0 dinosaurs of age=25. because my Android UI depends on this, to proceed to the next step.

Firebase ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
Query queryRef = ref.orderByChild("height").equalTo(25);

queryRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot snapshot, String previousChild) {
        System.out.println(snapshot.getKey());
    }
    // ....
});

EDIT Some solutions provided are suggesting to use ValueEventListener. But the problem is even if you use valueEventListener, in the above case, it still does not work,as there are 0 rows. The onDataChange does not fire.

Firebase ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
Query queryRef = ref.orderByChild("height").equalTo(25);

queryRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChanged(DataSnapshot snapshot) {
        System.out.println(snapshot.getKey());
    }
    // ....
});

ANSWER

@Override
        public void onDataChange(DataSnapshot snapshot) {
            //DinosaurFacts facts = snapshot.getValue(DinosaurFacts.class);
            //Log.d("hz-dino", facts.toString());

            if(snapshot.getValue() != null)
            {
                Log.d("hz-dino", snapshot.getKey());
                Log.d("hz-dino", String.valueOf(snapshot.getValue()));
            }
            else
            {
                Log.d("hz-dino", "there are exactly 0 rows!");
            }
        }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
srinivas
  • 4,778
  • 2
  • 32
  • 43
  • 1
    A firebase query is never "finished". When you're using a child added listener, every child needs to be treated the same. There is no initial state. – Anid Monsur Oct 12 '15 at 17:30
  • Possible duplicate of [Test if a data exist in Firebase](http://stackoverflow.com/questions/24824732/test-if-a-data-exist-in-firebase) – Kato Oct 14 '15 at 22:27
  • Hey guys, I don't understand. I want the results of a query. So how can I check for a value listener when there are 0 rows? @Kato – srinivas Oct 15 '15 at 09:46
  • You test if data exists. If it doesn't exists, there are zero rows. This is a duplicate and the link is your answer. – Kato Oct 15 '15 at 20:01

1 Answers1

6

To test for data:

snapshot.getValue() != null

When using

ref.addValueEventListener

If no data exists at the location, the snapshot will return null.

Jay
  • 34,438
  • 18
  • 52
  • 81
  • 3
    `onChildAdded` and this code would never be called if there were no children. – Anid Monsur Oct 12 '15 at 17:32
  • 1
    But this wont work in a query form when you want to know there are exactly 0 rows for that query. – srinivas Oct 12 '15 at 17:45
  • 1
    If the snapshot returns null, then there are 0 'rows' which would indicate nothing matched the parameters of the query. Is there some other result that would be expected? – Jay Oct 12 '15 at 18:13
  • @Jay Even with ref.addValueEventListener, onDataChange(DataSnapshot snapshot) will not get triggered as there are no matches to the query.. So there is no ending condition of the listener and hence the problem. – srinivas Oct 12 '15 at 19:42
  • 1
    @srinivas That's not true. A value listener will always be called, even if there's no data. Child added on the other hand, would only be called if there were children. – Anid Monsur Oct 12 '15 at 21:45
  • Hey guys, I don't understand. I want the results of a query. So how can I check for a value listener when there are 0 rows? @FrankvanPuffelen – srinivas Oct 15 '15 at 09:46
  • 1
    The initial question: how do I know if a query returns 0 rows. Returning 0 rows would indicate no results (no rows) were returned. There would be no value since there are no results. When you perform a query, the code within the query block is always called (check me on this) - you just have to take into account that it might return no data so you don't try to read the snapshot.value when it's null (that code would blow up). So just a basic if...then block handles that. – Jay Oct 15 '15 at 14:36