我在 Cloud Firestore 中有 2 个不同的集合:
- 帐户(标识符:userId)
- 用户名
- 图片URL
- 帖子(标识符:id)
- 用户ID
- 文本
- 喜欢
目标:我希望将这两个集合放在 Fragment 中,并使用 Androidx RecyclerView 来显示带有用户名和图片 URL 的帖子。
我在 build.gradle 中使用了“androidx.recyclerview:recyclerview:1.1.0”。
我使用以下方法从 Firebase 获取帖子并将其存储在模型“FirebasePost”中:
/**
* Get all FirebasePost objects from Firebase Firestore collection
* @return
*/
public FirestoreRecyclerOptions<FirebasePost> getPosts() {
Query mQuery = mFirestore.collection("posts").orderBy("createdAt", Query.Direction.DESCENDING);
// Get the response elements
FirestoreRecyclerOptions<FirebasePost> response = new FirestoreRecyclerOptions.Builder<FirebasePost>()
.setQuery(mQuery, FirebasePost.class)
.build();
return response;
}
我通过以下代码填充 RecyclerView:
/**
* Prepares the Recycler View for showing the Posts data
* @param mView Gets the current view
*/
private void prepareRecyclerView(View mView, FirestoreRecyclerOptions<FirebasePost> response) {
adapter = new FirestoreRecyclerAdapter<FirebasePost, PostHolder>(response) {
@Override
public void onBindViewHolder(@NonNull PostHolder holder, int position, @NonNull FirebasePost model) {
// Bind the FirebasePost object to the PostHolder
holder.setPostText_to_UI(model.getText());
holder.setTimestamp_to_UI(model.getCreatedAt());
holder.setAuthorDisplayName_to_UI(model.getAuthorUserId());
}
@NonNull
@Override
public PostHolder onCreateViewHolder(@NonNull ViewGroup group, int i) {
// Create a new instance of the ViewHolder, in this case we are using a custom
// layout called R.layout.message for each item
View view = LayoutInflater.from(group.getContext())
.inflate(R.layout.posts_item_cardview, group, false);
return new PostHolder(view);
}
};
mPostsRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mPostsRecyclerView.setItemAnimator(new DefaultItemAnimator());
mPostsRecyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
如何向 Firebase 实现第二个请求以获取用户信息并将这些信息添加到 RecyclerView 的正确位置? 问题是,我得到了 AuthorUserId,但没有得到用户的 displayName,因为在将模型提供给 RecyclerView 之前,我无法向 Firestore 执行另一个请求。
最佳答案
I want to have these two collections in Fragment with an AndroidX
RecyclerView
to show the posts with the username andpictureURL
.
Firestore 中的查询是浅层查询,这意味着您只能从运行查询的集合中获取项目。无法在单个查询中从不同集合中获取文档。在 NoSQL 世界中,不存在基于两个表中存在的 id 的 INNER JOIN 概念。 Firestore 不支持一次性跨不同集合进行查询,除非您使用 Firestore collection group queries ,但事实并非如此。单个查询只能使用单个集合中文档的属性。
我能想到的最简单的解决方案是在每个帖子下存储用户的数据(用户名和图片URL)。这种做法称为非规范化,是 Firebase 的常见做法。如果您不熟悉 NoSQL 数据库,我建议您观看此视频,Denormalization is normal with the Firebase Database为了更好的理解。它适用于 Firebase 实时数据库,但相同的规则适用于 Cloud Firestore。
有关更多信息,我建议您还阅读我在以下帖子中的回答:
编辑:
复制数据时,需要记住一件事。同样的,你在添加数据,你需要维护它。换句话说,如果您想要更新/删除某个项目,您需要在该项目存在的每个位置执行此操作。这就是该技术的工作原理。
I want to avoid to iterate over all (maybe millions of) posts and delete the duplicated user data there.
对于数据库中的大量更新/删除,每次都是一种权衡,但这始终取决于您的具体用例和要求。另一种解决方案可能是查询数据库两次,一次获取帖子,然后获取相应的用户详细信息。这项技术并不像您想象的那么慢。所以你也应该考虑将其作为一种选择。
您还应该考虑的另一个解决方案是增强数据以接受反向查找。也就是说,您可以在每个用户对象下添加子集合中相应的帖子。如果需要查询所有用户的所有帖子,则需要进行集合组查询。
关于java - 使用 Firebase Firestore 中两个集合的数据填充 Androidx RecyclerView 和 CardView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60801247/