领域 Java 迁移:属性 已成为必需

Realm Java Migration: Property has been made required

适用于大多数全新安装,但在最新的应用程序更新后出现了很多关于此问题的报告。更新之间已经有一段时间了,所以我认为这可能是由于用户在我添加模式之前从非常旧的版本更新造成的......但其他人说他们已经从全新安装中看到了这一点。领域 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;
}

Double 表示 nullable double.

double 表示 non-null double.

所以.addField("loanRatePct", Double.class);应该是.addField("loanRatePct", double.class);