Firebase Firestore order by not working

大憨熊 提交于 2021-02-07 12:38:10

问题


I am learning about Firebase Firestore through official documentation. I was trying following code. Notifications are added using add() method of firestore.

FirebaseFirestore.getInstance().colRefNotifications()
            .orderBy("timestamp", Query.Direction.DESCENDING)
            .get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
        @Override
        public void onSuccess(QuerySnapshot documentSnapshots) {

            if (!documentSnapshots.isEmpty()){

                listNotifications.clear();
                recyclerViewNotification.removeAllViews();

                for (DocumentSnapshot data : documentSnapshots){

                    Notification notification = data.toObject(Notification.class);
                    listNotifications.add(notification);
                }

                notificationGeneralAdapter.notifyDataSetChanged();
            }
        }
    });

Notification.java

private String text;
private String key;
private Date timestamp;

public Notification() {
}

public Notification(String text, String key, Date timestamp) {
    this.text = text;
    this.key = key;
    this.timestamp = timestamp;
}

public String getText() {
    return text;
}

public String getKey() {
    return key;
}

public Date getTimestamp() {
    return timestamp;
}

Firestore

I am ordering notifications by timestamp in descending direction. But, as you can see in the following snapshot, it is not showing desired output.

What am I doing wrong? Thank you for your help.


回答1:


This is happening because of nested queries. If you put a query inside a listener (success/failure/complete), orderBy method will not work. Place your next query inside onBindViewHolder of the RecyclerView adapter.

FirebaseFirestore.getInstance().colRefNotifications()
        .orderBy("timestamp", Query.Direction.DESCENDING)
        .get()
        .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot documentSnapshots) {

        if (!documentSnapshots.isEmpty()){
           ....

           /**If you are nesting queries like this, move second query to**/
           /**onBindViewHolder of the RecyclerView adapter**/

           FirebaseFirestore.getInstance().colRefUsers()
           .get()
           .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
           @Override
           public void onSuccess(QuerySnapshot documentSnapshots) {

               if (!documentSnapshots.isEmpty()){
               ....

               }
             }
          });
        }
    }
});



回答2:


This is happening because you are not using the timestamp as server value timestamp. You are setting the timestamp using the Date class this is not how you must do it.

Here is how you can add the timestamp using a model class. So, change your model class according with my answer from that post, also don't forget the @ServerTimestamp annotation and then your query will work fine.



来源:https://stackoverflow.com/questions/48624893/firebase-firestore-order-by-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!