向 Firebase DB 添加新字段时的向后兼容性
Backward compatibility while adding new fields to Firebase DB
我当前的 Firebase 实时数据库中有以下数据结构:
public class E {
public int x;
public int y;
public int x;
}
我的 Android 应用程序当前正在使用此数据库,用户已在生产中使用该数据库。
我想添加一个新成员到 class(字符串 z),如下所示:
public class E {
public int x;
public int y;
public int x;
public String z;
}
使用 旧版本 应用时,我在 logcat 中看到此错误:
W/ClassMapper: No setter/field for z found on class com.me.you.E
而且应用卡住了。
我在应用程序启动之前包含了数据库版本检查以引导用户更新他们的应用程序,但希望在这种特定情况下不需要它。
是否可以对数据进行版本控制?还有其他建议吗?
我认为您可能没有添加与新变量 z 相关的 getter 和 setter 方法。
要删除该警告消息,请使用 @IgnoreExtraProperties
:
注释您的 class
@IgnoreExtraProperties
public class E {
public int x;
public int y;
public int x;
}
见reference documentation for the IgnoreExtraProperties
annotation:
Properties that don't map to class fields are ignored when serializing to a class annotated with this annotation.
正如 Doug 评论的那样,除了这个消除警告的简单更改之外,Firebase 上的数据迁移是一个复杂的主题,不可能在一个 Stack Overflow 问题中回答。但我们 可以 尝试帮助解决特定问题,例如警告和崩溃。如果您的应用程序仍然无法加载,请 post a minimal complete verifiable way to reproduce the problem.
我当前的 Firebase 实时数据库中有以下数据结构:
public class E {
public int x;
public int y;
public int x;
}
我的 Android 应用程序当前正在使用此数据库,用户已在生产中使用该数据库。
我想添加一个新成员到 class(字符串 z),如下所示:
public class E {
public int x;
public int y;
public int x;
public String z;
}
使用 旧版本 应用时,我在 logcat 中看到此错误:
W/ClassMapper: No setter/field for z found on class com.me.you.E
而且应用卡住了。
我在应用程序启动之前包含了数据库版本检查以引导用户更新他们的应用程序,但希望在这种特定情况下不需要它。
是否可以对数据进行版本控制?还有其他建议吗?
我认为您可能没有添加与新变量 z 相关的 getter 和 setter 方法。
要删除该警告消息,请使用 @IgnoreExtraProperties
:
@IgnoreExtraProperties
public class E {
public int x;
public int y;
public int x;
}
见reference documentation for the IgnoreExtraProperties
annotation:
Properties that don't map to class fields are ignored when serializing to a class annotated with this annotation.
正如 Doug 评论的那样,除了这个消除警告的简单更改之外,Firebase 上的数据迁移是一个复杂的主题,不可能在一个 Stack Overflow 问题中回答。但我们 可以 尝试帮助解决特定问题,例如警告和崩溃。如果您的应用程序仍然无法加载,请 post a minimal complete verifiable way to reproduce the problem.