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

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...