gpt4 book ai didi

java - 如何在云Firestore中使用Firebase正确重写代码?

转载 作者:行者123 更新时间:2023-12-01 18:08:03 26 4
gpt4 key购买 nike

我写信是为了连接到 Firebase,现在我想将所有内容传输到云端 Firestore1)第一个方法是为了从firebase获取“Comment”而编写的

private void iniRvComment() {
RvComment.setLayoutManager(new LinearLayoutManager(this));
DatabaseReference commentRef = firebaseDatabase.getReference(COMMENT_KEY).child(postKey);
commentRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
listComment = new ArrayList<>();
for(DataSnapshot snapshot: dataSnapshot.getChildren()){
Comment comment = snapshot.getValue(Comment.class);
listComment.add(comment);
}
commentAdapter = new CommentAdapter(getApplicationContext(), listComment);
RvComment.setAdapter(commentAdapter);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}

2) 以及如何重写此代码以从云 Firestore 获取“评论”。我下面写的不正确

private void iniRvComment() {
RwComment.setLayoutManager(new LinearLayoutManager(this));
DocumentReference docRef = firestore.collection("Comment").document(postKey);
docRef.collection("Comment").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
if (documentSnapshot != null && !documentSnapshot.getDocuments().isEmpty()) {
listComment = new ArrayList<>();
List<DocumentSnapshot> documents = documentSnapshot.getDocuments();
for (DocumentSnapshot value : documents) {

Comment comment = value.toObject(Comment.class);
listComment.add(comment);
}
commentAdapter = new CommentAdapter(getApplicationContext(), listComment);
RwComment.setAdapter(commentAdapter);
}
}
});
}

enter image description here

最佳答案

我建议将您的评论扁平化到单个顶级 Comments 集合中,而不是将它们存储在帖子下。这将允许您执行许多有用的搜索操作,例如按用户或帖子搜索所有评论,甚至创建“最近活跃的帖子”提要。

要实现这一点,您需要更改数据库结构,以便所有评论都与它们所附加的帖子一起存储。

{
content: "...",
timestamp: 1234534568425,
postId: "...",
uid: "...",
uimg: "...",
uname: "..."
}

完成此操作后,您现在可以使用以下方式通过帖子查询评论:

private final int PAGE_SIZE = 10;
private void iniRvComment() {
RvComment.setLayoutManager(new LinearLayoutManager(this));

firestore.collection("Comments") // this is a top level collection
.whereEqualTo("postId", postKey) // select comments for given post
.orderBy("timestamp", Query.Direction.DESCENDING) // order newest to oldest
.limit(PAGE_SIZE) // fetch up to PAGE_SIZE recent comments
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot results,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.w(TAG, "Listen failed.", e);
return;
}

listComment = new ArrayList<>(PAGE_SIZE);
for (QueryDocumentSnapshot commentDoc : results) {
Comment commentObj = commentDoc.toObject(Comment.class);
listComment.add(commentObj);
}

commentAdapter = new CommentAdapter(getApplicationContext(), listComment);
RvComment.setAdapter(commentAdapter);
Log.d(TAG, "Retrieved " + results.size() + " recent comments.");
}
});
}

关于java - 如何在云Firestore中使用Firebase正确重写代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60521511/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com