Migration is required due to the following errors: Property has been added

1.9k Views Asked by At

I added a new property called 'productsList' of type RealmList< String > to the class which extends RealmObject. THe problem is that when I run the application I get a crash and this error message:

io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors: - Property 'FuelStationBean.productsList' has been added.

I have a class RealmMigration which implements RealmMigration:

public class RealmMigration implements io.realm.RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
    RealmSchema schema = realm.getSchema();

    if (oldVersion == 0) {
        schema.get("FuelStationBean")
                .addField("ladestation", boolean.class)
                .addField("ladepunkt", String.class);
        oldVersion++;
    }
}

}

And in a class AppController I have this function which initiates Realm:

protected static synchronized void initRealm(Context context) {
    if (!isRealInitialized) {
        // Initialize Realm
        Realm.init(context);

        RealmConfiguration config = new RealmConfiguration.Builder()
                .schemaVersion(1) 
                .migration(new RealmMigration()) 
                .build();

        Realm.setDefaultConfiguration(config);

        //Realm realm = Realm.getInstance(config);
        //realm.close();

        isRealInitialized = true;
    }
}

My question is: How should I do the migration so the app will work? I tried updating the the schemaVersion from 1 to 2 in initRealm function but didn't work. I know there are other topics on this subject but couldn't find a proper solution for this issue. Thanks

0

There are 0 best solutions below