Android:链接到 Cloud Firestore 的嵌套 RecyclerView 的最佳实践

Android: best practice for nested recyclerview linked to cloud firestore

我目前使用嵌套的 RecyclerView 和 SQLite 数据库。但是,我目前正在切换到 firebase,使用 Cloud Firestore 作为数据库。

我的设置是:

    DocumentSnapshot snapshot = getSnapshots().getSnapshot(verticalPosition);

    String roomId = snapshot.getId();
if (currentUser != null) {
        String userID = currentUser.getUid();
        userRef = fdb.collection("users").document(userID);
        houseRef = userRef.collection("houses").document("house0");
        roomRef = houseRef.collection("rooms").document(roomId);

            roomRef.get().addOnCompleteListener(tasksnap -> {
                if (tasksnap.isSuccessful()) {
                    DocumentSnapshot document = tasksnap.getResult();
                    if (document.exists()) {

                        VerticalModel room = document.toObject(VerticalModel.class);
                        if (room != null) {
                            ArrayList<HorizontalModel> tasks = room.getArrayList();

                            hAdapter = new HorizontalRecyclerViewAdapter(context, new ArrayList<>(),
                                    verticalPosition, listener, cleanInter);
                            holder.view.rvHorizontal.setHasFixedSize(true);
                            holder.view.rvHorizontal.setLayoutManager(new LinearLayoutManager(context,
                                    LinearLayoutManager.HORIZONTAL, false));

                            holder.view.rvHorizontal.setAdapter(hAdapter);
                            holder.view.rvHorizontal.setNestedScrollingEnabled(false);

                            holder.updateContents(tasks, hAdapter);
                        }
                    }
                }
            });
        }

我将为一个任务切换到一个文档,而不是每个房间一个包含所有任务的数组列表。

但是,我遇到了很多 Firestore 读取调用,可能是因为 onbind 方法被调用了几次。

所以如果有高手:有没有更好的办法呢?不幸的是,使用 2 个 Firestore 适配器对我不起作用。

也许可以强制 firebase 一直从本地缓存中读取,并且只有当用户退出应用程序时,它才会更新更改?

根据您的第一条评论:

the number of reads and if there is a better way to implement the second rv

第二条评论:

Free users can only add 5 rooms, each with 5 tasks. Paid users will mostly add around 8 rooms, each with around 10 to 20 tasks.

我能想到的最简单的选择是在每个 Room 对象中使用一个数组类型字段来容纳所有 Task 对象。由于它只存在几个 Task 对象,您可以轻松地将它们添加到单个文档中。这样一来,显示一个房间连同所有任务,只需要支付一次阅读费用。

如果您想知道如何将自定义对象数组映射到列表中,请阅读以下文章: