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?

3
  • Can you show the child event listener?. It has 5 callback method and at least the return the value which is passed in startAt(), in method onChildAdded(), if there are no other results. Commented Aug 23, 2017 at 5:00
  • show how you are getting the firebase ref. Commented Aug 23, 2017 at 5:06
  • you can't use startAt on ID , you can use it on value of key under ID Commented Aug 23, 2017 at 5:19

2 Answers 2

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")...
Sign up to request clarification or add additional context in comments.

2 Comments

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
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. If you keep having problems, update your question with the code that reproduces the problem.
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.
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.