Firestore DocumentReference序列化 [英] Firestore DocumentReference Serialization
问题描述
我有一个pojo,可以映射一个Firestore文档.该文档包含对其他集合的其他文档的引用.
I have a pojo which maps a firestore document. That document contains references to other documents of other collection.
class Game(var local: String? = null, var visitor: String? = null,
var events: MutableList<DocumentReference> = mutableListOf(), var date: Date? = null): Serializable
问题在于DocumentReference无法序列化.我已经读到可以将路径另存为String,然后自己创建对象(FirebaseFirestore.getInstance().document(path)),但是在Firestore中,该字段不再是Reference类型的字段.
The problem is that DocumentReference isn't serializable. I've read that I can save the path as String and then create the object myself(FirebaseFirestore.getInstance().document(path)), but then in the firestore is no longer a field of type Reference.
所以我的问题是,这是好方法吗?另外,该字段是字符串而不是引用是否重要?
So my question, Is this the good approach? Also, Does it matter that the field is a String rather than a Reference?
问候,迭戈
推荐答案
所以我的问题是,这是个好方法吗?
So my question is, is this the good approach?
是的. DocumenetReference
中最重要的部分是字符串路径.如果将其序列化,则可以很容易地重新构造DocumentReference对象.
Yes it is. The most important part in a DocumenetReference
is the string path. If you serialize that, you can reconstitute a DocumentReference object, very easy.
结论是,如果您需要序列化DocumentReference
对象,请使用DocumentReference的 FirebaseFirestore.getInstance().document(path).
As the conclusion, if you need to serialize a DocumentReference
object, use DocumentReference's getPath() method to get a literal String that represents the document's location in the database. To deserialize that path String back into a DocumentReference
object, simply use FirebaseFirestore.getInstance().document(path).
这意味着数据库中的字段现在是String而不是Reference.
This implies that the field in the database is now a String instead of Reference.
是的.
有关系吗?我认为作为参考必须具有某种优势.
Does it matter? I suppose that being a Reference must have some type of advantage.
Yes, since now the property is of type a String, you cannot take advantage of what DocumentReference class provides anymore. But this is the workaround that can help you achieve what you want. String class is serializable while DocumentReference's is not.
这篇关于Firestore DocumentReference序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!