Android:链接到 Cloud Firestore 的嵌套 RecyclerView 的最佳实践
Android: best practice for nested recyclerview linked to cloud firestore
我目前使用嵌套的 RecyclerView 和 SQLite 数据库。但是,我目前正在切换到 firebase,使用 Cloud Firestore 作为数据库。
我的设置是:
- Firestore 数据库设置为:用户 -> 房屋 -> 房间 -> 任务
- 垂直 rv = 房间,水平 rv = 任务
- 立式房车一个型号,卧式房车一个型号
- 作为垂直 rv,我正在使用 FirestoreRecyclerAdapter
- 在我调用的 onbindviewholder 中:
DocumentSnapshot snapshot = getSnapshots().getSnapshot(verticalPosition);
String roomId = snapshot.getId();
- 使用我正在排队等待水平数组列表(任务)的 ID,它位于 Firestore 参考“房间”内:
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 对象,您可以轻松地将它们添加到单个文档中。这样一来,显示一个房间连同所有任务,只需要支付一次阅读费用。
如果您想知道如何将自定义对象数组映射到列表中,请阅读以下文章:
我目前使用嵌套的 RecyclerView 和 SQLite 数据库。但是,我目前正在切换到 firebase,使用 Cloud Firestore 作为数据库。
我的设置是:
- Firestore 数据库设置为:用户 -> 房屋 -> 房间 -> 任务
- 垂直 rv = 房间,水平 rv = 任务
- 立式房车一个型号,卧式房车一个型号
- 作为垂直 rv,我正在使用 FirestoreRecyclerAdapter
- 在我调用的 onbindviewholder 中:
DocumentSnapshot snapshot = getSnapshots().getSnapshot(verticalPosition);
String roomId = snapshot.getId();
- 使用我正在排队等待水平数组列表(任务)的 ID,它位于 Firestore 参考“房间”内:
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 对象,您可以轻松地将它们添加到单个文档中。这样一来,显示一个房间连同所有任务,只需要支付一次阅读费用。
如果您想知道如何将自定义对象数组映射到列表中,请阅读以下文章: