Realm Java Migration: Property has been made required
Asked Answered
E

1

13

Works fine for most fresh installs, but a lot of reports are coming in with this issue after the latest app update. It has been awhile between updates, so I THINK this could be caused by users updating from a very old version before I added schema...but others have said they have seen this from a fresh install. Realm Java is realm-gradle-plugin:5.1.1

I appreciate any help, thanks!

The Error:

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

Edit: forgot:

Loan.java

public class Loan extends RealmObject {
    String theloan;
    int loan;
    int loanRate;
    double loanRatePct;
    int loanBalance;
}
Elsyelton answered 12/6, 2018 at 2:18 Comment(1)
.addField("loanRatePct", Double.class); should be .addField("loanRatePct", double.class);Gabriellia
G
29

Double means nullable double.

double means non-null double.

So .addField("loanRatePct", Double.class); should be .addField("loanRatePct", double.class);.

Gabriellia answered 12/6, 2018 at 8:6 Comment(5)
note that you might need to bump the schema version and check if the field is nullable, and if so, then setRequired("loanRatePct", true)Gabriellia
You need to use ::class.javaPrimitiveType for the non-null version in Kotlin.Gabriellia
Thanks for the clarification. It's working. For those looking for an answer, you should use the primitive data-type instead of the Object in the second Parameter.Frankly
Does this also apply to Long, and Integer?Viscosity
Yes, it does apply to those as well I thinkGabriellia

© 2022 - 2024 — McMap. All rights reserved.