随着应用程序的进展,领域迁移量增加了很多。是否有任何可以显着减少迁移编码的优化设计模式?假设我有以下迁移:
if (!schema.contains(Subject.class.getSimpleName())) {
RealmObjectSchema inputConfigSchema = schema.create(Subject.class.getSimpleName())
.addField("name", String.class)
.addField("difficulty", String.class);
if (!schema.get(Student.class.getSimpleName()).hasField("subject")) {
schema.get(Student.class.getSimpleName()).addRealmObjectField("subject", inputConfigSchema);
}
}
if (!schema.get(Student.class.getSimpleName()).hasField("age")) {
schema.get(Student.class.getSimpleName()).addField("age", Integer.class);
schema.get(Student.class.getSimpleName()).setNullable("age", false);
}
}
请建议一个合适的设计模式来优化这种类型的领域迁移
答案 0 :(得分:0)
使用Student.class.getSimpleName()
获取字符串"Student"
将失败:
使用了Proguard并且该类被混淆
如果从项目中删除Student
类
RealmObjectSchema student = schema.get("Student")
if (!schema.contains("Subject")) {
RealmObjectSchema subject = schema.create("Subject")
.addField("name", String.class)
.addField("difficulty", String.class);
if (student.hasField("subject")) {
student.addRealmObjectField("subject", subject);
}
}
if (!student.hasField("age")) {
student.addField("age", Integer.class, FieldAttributes.REQUIRED);
}