【问题标题】:Realm migration: suitable code structure and design pattern领域迁移:合适的代码结构和设计模式
【发布时间】:2018-03-12 05:31:05
【问题描述】:

随着应用程序的进步,领域迁移量会大大增加。是否有任何优化的设计模式可以显着减少迁移的编码?假设,我有以下迁移:

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);
                        }
    }

请建议一个合适的设计模式来优化这种类型的领域迁移

【问题讨论】:

    标签: java android design-patterns realm realm-migration


    【解决方案1】:

    使用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);
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-16
      相关资源
      最近更新 更多