我正在如下使用Cloud Firestore:
集合“events”保存使用唯一事件ID作为名称的文档。在这些文档中有许多“EventComment”对象,每个对象代表用户发表的评论。要将“EventComment”对象添加到文档中,我使用以下代码:
EventComment mcomment = new EventComment();
mcomment.setComment("this is a comment");
Map<String, EventComment> eventMap = new HashMap<>();
eventMap.put(Long.toHexString(Double.doubleToLongBits(Math.random())), mcomment);
firestore.collection("events").document(event_id)
.set(eventMap, SetOptions.merge()).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(EventCommentsActivity.this, "ya bastud", Toast.LENGTH_SHORT).show();
}
});
我创建了一个由字符串和我的对象“EventComment”组成的HashMap,并将其设置在文档中。
然而,当我想检索给定文档中包含的所有“EventComment”对象时,我无法将其转换为EventComment对象,即我不能这样做:
DocumentReference docRef = db.collection("events").document("vvG17ZfcLFVna8");
docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
EventComment comment = documentSnapshot.toObject(EventComment.class);
}
});
此外,尝试将documentSnapshot转换为HashMap会出现“unchecked cast”消息,并最终失败。思路是使用注释来填充RecyclerView。
我在考虑是否需要重新构建数据存储方式?
非常感谢您的任何建议。
干杯