【问题标题】:Using .toObject with Generic classes in Firestore to get POJO from DocumentSnapshot在 Firestore 中使用 .toObject 和通用类从 DocumentSnapshot 获取 POJO
【发布时间】:2019-01-27 05:11:55
【问题描述】:

在 Firebase 实时数据库中,支持使用 GenericTypeIndicator 反序列化对象。有人可以阐明如何对 Firestore 做同样的事情吗?

我有一个文档快照,我想将其转换为参数化的 POJO。

这是我尝试这样做时引发的错误

java.lang.RuntimeException: Could not deserialize object. Class com.thesportsbeing.thesportsbeing.screens.festadmin.eventslist.eventadmin.scoring.MatchScore has generic type parameters, please use GenericTypeIndicator instead. 

问题是 firestore 包中没有 GenericTypeIndicator,它存在于 realtime-database 包中。任何解决方法?或者完成一些事情会很棒。

MyClass 和这个类似

public class MyClass<R extends SomeAbstractClass> {
    R someClassWhichExtendsAboveAbstractClass;
}

【问题讨论】:

  • 有什么解决办法吗?

标签: java firebase google-cloud-firestore


【解决方案1】:

以下是对问题和解决方法的讨论: https://github.com/firebase/firebase-android-sdk/issues/222

最简单的方法是声明泛型类的子类:

class MyNonGeneric extends MyClass<R> {}

// Usage
snapshot.toObject(MyNonGeneric.class);

或者需要单独获取 POJO 的每个顶级属性并进行转换。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-28
    • 2020-05-10
    • 2018-08-02
    • 2018-03-19
    • 2018-07-07
    • 2020-03-26
    • 2021-04-27
    • 1970-01-01
    相关资源
    最近更新 更多