Firebase Firestore订单不工作

Pra*_* ED 6 android firebase google-cloud-firestore

我通过官方文档了解Firebase Firestore.我正在尝试使用代码.使用firestore的add()方法添加通知.

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();
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

公司的FireStore

firestore_snapshot

我按时间戳向下排序通知.但是,正如您在下面的快照中看到的那样,它没有显示所需的输出.

快照

我究竟做错了什么?谢谢您的帮助.

Pra*_* ED 5

这是由于嵌套查询而发生的。如果将查询放入侦听器中(成功/失败/完成),orderBy 方法将不起作用。将下一个查询放入 RecyclerView 适配器的 onBindViewHolder 中。

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()){
               ....

               }
             }
          });
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 你能更详细地解释一下吗,我也面临着同样的问题。 (2认同)