Firestore DocumentReference序列化 [英] Firestore DocumentReference Serialization

查看:60
本文介绍了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?

问候,迭戈

推荐答案

所以我的问题是,这是个好方法吗?

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的

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆