本文介绍了Firestore DocumentReference序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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?

问候,迭戈

推荐答案

是的. 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的 getPath()方法,以获取表示文档在数据库中位置的文字字符串.要将路径字符串反序列化回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).

是的.

是的,因为现在该属性的类型为字符串,所以您不能利用 DocumentReference 类提供了.但这是可以帮助您实现所需目标的解决方法.字符串类可序列化,而DocumentReference则不能.

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序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 19:02