gpt4 book ai didi

android - Firestore 文档引用序列化

转载 作者:行者123 更新时间:2023-11-29 23:21:07 25 4
gpt4 key购买 nike

我有一个映射 firestore 文档的 pojo。该文档包含对其他集合中其他文档的引用。

class Game(var local: String? = null, var visitor: String? = null,
var events: MutableList<DocumentReference> = mutableListOf(), var date: Date? = null): Serializable

问题是 DocumentReference 不可序列化。我读到我可以将路径保存为字符串,然后自己创建对象(FirebaseFirestore.getInstance().document(path)),但是在 firestore 中不再是引用类型的字段。

所以我的问题是,这是好的方法吗?此外,字段是字符串而不是引用是否重要?

问候,迭戈

最佳答案

So my question is, is this the good approach?

是的。 DocumentetReference 中最重要的部分是字符串路径。如果对其进行序列化,则可以非常容易地重构一个 DocumentReference 对象。

作为结论,如果您需要序列化 ​​DocumentReference 对象,请使用 DocumentReference 的 getPath()方法来获取表示文档在数据库中的位置的文字字符串。要将该路径字符串反序列化回 DocumentReference 对象,只需使用 FirebaseFirestore.getInstance().document(path) .

编辑:

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.

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

关于android - Firestore 文档引用序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54288373/

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