gpt4 book ai didi

android - Firebase Firestore 订单不工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:02:43 25 4
gpt4 key购买 nike

我正在通过官方文档了解 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();
}
}
});

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

firestore_snapshot

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

Snapshot

我做错了什么?感谢您的帮助。

最佳答案

这是由于嵌套查询而发生的。如果将查询放在监听器中(成功/失败/完成),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()){
....

}
}
});
}
}
});

关于android - Firebase Firestore 订单不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48624893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com