com.google.firebase.database.DatabaseException:不支持序列化数组,请改用列表 [英] com.google.firebase.database.DatabaseException: Serializing Arrays is not supported, please use Lists instead

查看:155
本文介绍了com.google.firebase.database.DatabaseException:不支持序列化数组,请改用列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码保留自定义对象:

I am trying to persist a custom object using the following code:

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 注释添加到 bodyParts 列表中,一切正常我的练习课程,但这显然不是我想要实现的。

And every time I am getting this error - com.google.firebase.database.DatabaseException: Serializing Arrays is not supported, please use Lists instead. My objects don't contain any arrays so this error looks quite confusing. I found a way to fix this error - everything works fine if I add the @Exclude annotation to the bodyParts list from my Exercise class but it is obviously not what I am trying to achieve.

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

So seems that Firebase is unable to persist objects containing inner lists? Are there any straightforward workarounds or best practices here? Thanks!

P.S。我正在使用 firebase-database:9.2.1

P.S. I am using firebase-database:9.2.1

推荐答案

我我设法找到造成这次崩溃的原因。我在另一台运行Android 5.x的设备上安装了该应用,Firebase在那里引发了一个更加混乱的例外:

I've managed to find the reason causing this crash. I installed the app on another device running Android 5.x and Firebase threw a much less confusing exception there:

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

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

It appeared that Firebase (unlike Gson) tries to serialize all possible getters even though they don't have direct connection with global variables (class members / fields), in my particular case one of my objects contained a method returning Drawable - getDrawable(). Obviously, Firebase doesn't know how to convert drawable into json.

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

Interesting moment (probably a bug in Firebase SDK): on my older device running Android 4.4.1 I still get my original exception in the same project and configuration:

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

这篇关于com.google.firebase.database.DatabaseException:不支持序列化数组,请改用列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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