领域Java迁移:已经需要属性

时间:2018-06-12 02:18:39

标签: java android realm realm-java

适用于大多数全新安装,但在最新的应用更新后,很多报告都会出现此问题。在更新之间已经有一段时间了,所以我认为这可能是由于用户在添加模式之前从一个非常旧的版本更新...但其他人说他们已经从全新安装中看到了这一点。 Realm Java是realm-gradle-plugin:5.1.1

感谢您的帮助,谢谢!

错误:

Caused by io.realm.exceptions.RealmMigrationNeededException
Migration is required due to the following errors: - Property 'Loan.loanRatePct' has been made required.
...
...onCreate (Activity.java:168)

MyMigration.java

if (oldVersion == 0) {
    schema.get("Storage")
            .addField("heat", String.class);
    oldVersion++;
}

if (oldVersion == 1) {
    schema.get("Env")
            .addField("gameType", String.class);
    oldVersion++;
}

if (oldVersion == 2) {
    schema.get("Loan")
            .addField("loanRatePct", Double.class);
    schema.create("GameMode")
             .addField("md", String.class)
            .addField("name", String.class)
            .addField("days", int.class)
            .addField("term", int.class)
            .addField("rate", String.class)
            .addField("amount", int.class)
            .addField("icon", int.class);
    schema.create("Leaderboard")
            .addField("score", String.class);

    oldVersion++;
}

if (oldVersion == 3) {
    schema.get("Leaderboard")
            .addField("mode", String.class);
    oldVersion++;
}

MyApplication.java

Realm.init(getApplicationContext());
    RealmConfiguration config = new RealmConfiguration.Builder()
            //todo: EVERY UPDATE: is new schema needed?
            .schemaVersion(4) // Must be bumped when the schema changes
            .migration(new MyMigration()) // Migration to run instead of throwing an exception
            .build();
    Realm.setDefaultConfiguration(config);

Activity.java

protected void onCreate(Bundle savedInstanceState) {    
    realm = Realm.getDefaultInstance();
    ...
}

编辑:忘了:

Loan.java

public class Loan extends RealmObject {
    String theloan;
    int loan;
    int loanRate;
    double loanRatePct;
    int loanBalance;
}

1 个答案:

答案 0 :(得分:11)

Double表示nullable double

double表示non-null double

因此.addField("loanRatePct", Double.class);应为.addField("loanRatePct", double.class);