我想知道当我使用addSnapshotListener
的实时更新时,如何对新的FirebaseFirestore数据库进行分页。
我遇到的问题是,一旦设置了查询,我就无法更新limit()
,因此它保持相同的限制,我无法加载更多数据。
这就是我目前所拥有的:
private Query query = db.collection("groups").orderBy("id", Query.Direction.DESCENDING).limit(15);
这些是查询参数,我有一个ListenerRegistration
来删除数据库监听器(我知道它也可以将活动传递给addSnapshotListener
方法)。
这是获取数据的代码:
registration = query.addSnapshotListener(this, new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
mGroupArrayList.clear();
if (e != null) {
Log.w("MAINACTY", "listen:error", e);
return;
}
for (DocumentSnapshot dc : documentSnapshots.getDocuments()) {
// Get the last visible document
lastVisible = documentSnapshots.getDocuments()
.get(documentSnapshots.size() - 1);
mGroupArrayList.add(dc.toObject(Group.class));
}
adapter.notifyDataSetChanged();
}
});
如上所述,问题在于我无法更新我在查询中首次设置的限制,如果我尝试执行新查询,则在对信息进行编辑或离开时旧信息会消失活动。
在文档中只有一个使用get()
方法进行分页的示例,但使用该方法时没有实时更新。
答案 0 :(得分:1)
我在github上为Recyclerview Firestore分页制作此代码,这可能有助于解决您的问题。