Skip to content Skip to sidebar Skip to footer

Sorting Firebase Database By Timestamp Under A Key

I am new on firebase and while working, everything turns out to be great except for one tiny thing, How can we sort the values by timestamp on firebase database ?? I have data in f

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.

You need to take care of that list inside your onDataChange method by looping over dataSnapshot.getChildren():

another.orderByChild("timestamp").limitToLast(10).addListenerForSingleValueEvent(newValueEventListener() {
    @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
            for (DataSnapshotchildSnapshot: dataSnapshot.getChildren()) {
                String pw = childSnapshot.getValue().toString().trim();
                process(pw);toast(pw);
            }
        } else {
            gost("Conversation not started previously.");
        }
    }
    @OverridepublicvoidonCancelled(DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore errors
    }
});

Post a Comment for "Sorting Firebase Database By Timestamp Under A Key"