gpt4 book ai didi

android - 使用限制查询 Firebase 数据库

转载 作者:行者123 更新时间:2023-12-05 09:16:29 26 4
gpt4 key购买 nike

我刚刚开始使用 firebase,它非常有用。

我有这样的节点。

enter image description here

目前,我是这样写的,它会返回我所有的消息。如果我有 1k 条消息,它会返回 1k 条消息。我只想查询最新的 100 条消息。是否可以?我需要编写云功能吗?

private void retrieveConversation(String conversationID) {

Query queryRoom = FirebaseDatabase.getInstance().getReference().child("Conversation").child(conversationID);
queryRoom.keepSynced(true);
queryRoom.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Conversation conversation = dataSnapshot.getValue(Conversation.class);
if (conversation != null) {
conversation = updateConversation(conversation);
mainMessage.conversations.put(conversation.getId(), conversation);
EventBus.getDefault().post(new ChatAdapter.AddMessage());
}
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

最佳答案

Please try using limitToLast(int value) along with your query

Query queryRoom = FirebaseDatabase.getInstance().getReference().child("Conversation").child(conversationID).limitToLast(100);

Or you can sort them by using timeStamp like

FirebaseDatabase.getInstance().getReference()
.child("Conversation").child(conversationID)
.limitToLast(100)
.orderByChild("timestamp")
.startAt(dateToStart) // pass timestamp from when you want to start filtering
.endAt(dateToEnd); // pass timestamp till when you want to apply filtering

关于android - 使用限制查询 Firebase 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50445865/

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