Android Firebase Weird LimitToLast(1) Behaviour
I'm using Firebase for Android and faced some misbehaviour of limitToLast() query. What I'm trying to do, is to get the last child of list, and iterate through other children of th
Solution 1:
When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.
So your code will need to first find the draws/2
child (by looping) and then loop over the children:
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snap: dataSnapshot.getChildren()){
System.out.println(snap.getKey()); // prints 2
for (DataSnapshot snap2: snap.getChildren()){
//Some work
snap.getValue()
}
}
}
It's typically easier to handle query results by using addChildEventListener
, because then you'll get a separate call to onChildAdded
for each child (and can thus skip the extra loop).
Btw: there are a lot of reasons why the documentation for the Firebase Database doesn't use arrays and even recommends against using arrays. Ignore them at your own peril.
Post a Comment for "Android Firebase Weird LimitToLast(1) Behaviour"