微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – com.google.firebase.database.DatabaseException:不支持序列化数组,请改用列表

我试图使用以下代码持久化自定义对象:

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
DatabaseReference curWorkoutExercisesRef = databaseReference.child("workouts")
            .child(mCurrentWorkout.getId())
            .child("workoutExercises");

WorkoutExercise we = new WorkoutExercise(exercise);
curWorkoutExercisesRef.push().setValue(we);

这是我的目标:

public class WorkoutExercise {

    private String id;
    private Exercise exercise;

    public WorkoutExercise() {}

    // getters, setters, other methods 
    // ...
}

public class Exercise {

    private String id;
    private String title;

    private List<BodyPart> bodyParts;

    public Exercise() {}

    // getters, setters, other methods 
    // ...
}

public class BodyPart {

    private String id;
    private String name;

    public BodyPart() {}

    // getters, setters, other methods 
    // ...
}

每次我收到此错误时 – com.google.firebase.database.DatabaseException:不支持序列化数组,请改用列表.我的对象不包含任何数组,因此这个错误看起来很混乱.我找到了一种方法来修复这个错误 – 如果我将@Exclude注释添加到我的Exercise类的bodyParts列表中,一切正常,但显然不是我想要实现的.

所以似乎Firebase无法持久存在包含内部列表的对象?这里有任何简单的解决方法或最佳实践吗?谢谢!

附:我正在使用firebase-database:9.2.1

解决方法:

我设法找到导致这次崩溃的原因.我在另一台运行Android 5.x的设备上安装了该应用,Firebase在那里引发了一个不那么令人困惑的异常:

com.google.firebase.database.DatabaseException: No properties to serialize found on class android.graphics.Paint$Align

似乎Firebase(与Gson不同)尝试序列化所有可能的getter,即使它们没有与全局变量(类成员/字段)直接连接,在我的特定情况下,我的一个对象包含一个返回Drawable的方法 – getDrawable() .显然,Firebase不知道如何将drawable转换为json.

有趣的时刻(可能是Firebase SDK中的一个错误):在我运行Android 4.4.1的旧设备上,我仍然在同一个项目和配置中得到原始异常:

Caused by: com.google.firebase.database.DatabaseException: Serializing Arrays is not supported, please use Lists instead

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐