Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm Java Migration: Property has been made required

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;
}
like image 532
zngb Avatar asked Jun 12 '18 02:06

zngb


1 Answers

Double means nullable double.

double means non-null double.

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

like image 174
EpicPandaForce Avatar answered Oct 16 '22 17:10

EpicPandaForce