Android Firebase FireStore实时更新分页

问题描述 投票:0回答:1

我想知道当我使用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()方法进行分页的示例,但是使用该方法没有实时更新。

java android firebase pagination google-cloud-firestore
1个回答
1
投票

我在github上为Recyclerview Firestore分页制作此代码,也许这将有助于解决您的问题。

https://github.com/Malik333/RecyclerviewFirestorePagination

© www.soinside.com 2019 - 2024. All rights reserved.