0

enter image description here

I want to order by timeStamp the comments inside the comments node, get the last 15 messages starting with KsBazxx0bew6UOfUI5J which is a child of the comments node. I´m trying to get the comments using the following code:

FirebaseRef.orderByChild("timeStamp").startAt(commentID).limitToLast(15).addChildEventListener(likeOrCommentsListener);

unfortunetly I'm not able to get any information, which makes me think that the query is wrong. How can I get the messages using those restrictions?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Andrey Solera
  • 2,311
  • 2
  • 26
  • 51

2 Answers2

2

Since you're ordering by timestamp, you need to pass in the value of the timestamp where you want to start the query:

FirebaseRef.orderByChild("timeStamp").startAt(150317519355)...

If there may be multiple items with the same timestamp, you can additionally specify that the query should start at a specific key:

FirebaseRef.orderByChild("timeStamp").startAt(150317519355, "KsBazxx0bew6UOfUI5J")...
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • thanks for such a quick response, I only have a little problem, it says that the timeStamp `150317519355` is too large to add it i the `startAt()`, or am I doing something wrong? Many thanks – Andrey Solera Aug 23 '17 at 14:13
  • I copied the value from your screenshot (next time post the JSON as text please). I might have made a typo, although I don't see it. If you can store a value as a number, you should be able to query it since [`startAt()` takes a `double`](https://stackoverflow.com/questions/43203910/how-to-create-negative-firebase-timestamp-in-swift/43394905#43394905). If you keep having problems, update your question with the code that reproduces the problem. – Frank van Puffelen Aug 23 '17 at 14:32
0

Try this

 DatabaseReference databaseReference = Firebase.getInstance().getReference();
Query lastQuery = databaseReference.child("comments").orderByChild("timeStamp").limitToLast(15);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
       //use this dataSnapshot
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        //Handle possible errors.
    }
});
Manohar
  • 22,116
  • 9
  • 108
  • 144