向后兼容,同时向Firebase DB添加新字段

时间:2018-06-29 19:42:04

标签: android firebase firebase-realtime-database database-versioning

我当前的Firebase实时数据库中具有以下数据结构:

public class E {
  public int x;
  public int y;
  public int x;
}

我的Android应用程序当前正在使用该数据库,生产环境中的用户已经在使用该数据库。

我想向类(字符串z)添加一个新成员,如下所示:

public class E {
  public int x;
  public int y;
  public int x;
  public String z;
}

使用该应用程序的旧版本时,我在logcat中看到此错误:

  

W / ClassMapper:在com.me.you.E类上找不到z的setter / field

应用程序被卡住。

在应用程序开始指导用户更新其应用程序之前,我已经进行了数据库版本检查,但是希望在这种特定情况下不需要它。

数据上可能有版本控制吗?还有其他建议吗?

2 个答案:

答案 0 :(得分:1)

我认为您可能未添加与新变量z相关的getter和setter方法。

答案 1 :(得分:1)

要摆脱该警告消息,请用@IgnoreExtraProperties注释您的班级:

@IgnoreExtraProperties
public class E {
  public int x;
  public int y;
  public int x;
}

请参见reference documentation for the IgnoreExtraProperties annotation

  

序列化到带有此注释的类时,将忽略未映射到类字段的属性。

正如Doug所评论的那样,除了摆脱警告的简单更改之外,Firebase上的数据迁移是一个复杂的主题,不可能在单个Stack Overflow问题中回答。但是我们可以尝试解决特定的问题,例如警告和崩溃。如果您的应用仍然无法加载,请发布minimal complete verifiable way to reproduce the problem

相关问题