RealmMigrationNeededException even with migration


#1

Hi, i am getting this exception for some user, i am sure i am setting the migration class to default realm config and also i added the fields to the model classes , but for some reason i am getting this error when i try to access to a realm instance
Oncreate in application class

  Realm.init(this);
        RealmConfiguration config =
                new RealmConfiguration.Builder()
                        .schemaVersion(3)
                        .migration(new TwnelMigration())
                        .build();
        Realm.setDefaultConfiguration(config);
public class TwnelMigration implements RealmMigration {
    @Override
    public void migrate(final DynamicRealm realm, long oldVersion, long newVersion) {
        RealmSchema realmSchema = realm.getSchema();
        // Migrate from version 1 to version 2
        if (oldVersion == 1) {
            RealmObjectSchema chatSchema = realmSchema.get("Chat");
            chatSchema
                    .addField("typing", boolean.class)
                    .addField("typingTime", long.class);
            oldVersion++;
        }
        else if (oldVersion == 2) {
            RealmObjectSchema chatSchema = realmSchema.get("Chat");
            RealmObjectSchema contactSchema = realmSchema.get("Contact");
            // Create a new class
            RealmObjectSchema affiliationSchema = realmSchema.create("Affiliation")
                    .addRealmObjectField("contact", contactSchema)
                    .addField("affiliation", int.class)
                    .addField("room", String.class);

            chatSchema.addRealmListField("affiliations", affiliationSchema);

            oldVersion++;
        }
       
    }
}

but in the documentation example for migration i see

if (oldVersion == 0) {
        schema.create("Person")
            .addField("name", String.class)
            .addField("age", int.class);
        oldVersion++;
     }

     // Migrate to version 2: Add a primary key + object references
     // Example:
     // public Person extends RealmObject {
     //     private String name;
     //     @PrimaryKey
     //     private int age;
     //     private Dog favoriteDog;
     //     private RealmList<Dog> dogs;
     //     // getters and setters left out for brevity
     // }
     if (oldVersion == 1) {
        schema.get("Person")
            .addField("id", long.class, FieldAttribute.PRIMARY_KEY)
            .addRealmObjectField("favoriteDog", schema.get("Dog"))
            .addRealmListField("dogs", schema.get("Dog"));
        oldVersion++;
     }

check the IF without the ELSE, the question is if it could be the reason for this exception ??


#2

Specify the exception stack trace so that we know what’s missing. Realm-Java includes detailed exception messages for “migration needed exceptions” for what is different compared to what is expected.

Also, default schema version is 0, not sure if that is handled (0 -> 1).