Kha*_*inn 1 android firebase firebase-realtime-database
我刚开始使用 firebase,它非常有用。
我有这样的节点。
目前,我是这样写的,它会返回我所有的消息。如果我有 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) {
}
});
}
Run Code Online (Sandbox Code Playgroud)
请尝试将 limitToLast(int value) 与您的查询一起使用
Query queryRoom = FirebaseDatabase.getInstance().getReference().child("Conversation").child(conversationID).limitToLast(100);
Run Code Online (Sandbox Code Playgroud)
或者您可以使用时间戳对它们进行排序
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5482 次 |
| 最近记录: |