RealmMigrationNeededException when changing Realm model

Whenever I change the model, as adding more fields, the application crashes with an io.realm.exceptions.RealmMigrationNeededException error. This can only be solved by uninstalling and reinstalling the application.

Any suggestion on migration? I use only the default instance.

+4
source share
3 answers

You can find the information you need:

https://realm.io/docs/java/latest/#migrations

Just changing the code in the new definition will work fine if you do not store data on disk according to the old database schema. But if you do, there will be a discrepancy between what Realm sees in the code & Realm data sees on disk, so an exception will be thrown.

+6

, Realm Configuration .

Realm realm = null;

                    try {
                       realm = Realm.getInstance(MainActivity.this);
                    } catch (RealmMigrationNeededException r) {
                        Realm.deleteRealmFile(MainActivity.this);
                        realm = Realm.getInstance(MainActivity.this);
                    }

RealmConfiguration config2 = new RealmConfiguration.Builder(this)
                .name("default2") 
                .schemaVersion(3) 
                .deleteRealmIfMigrationNeeded() 
                .build(); 


        realm = Realm.getInstance(config2);

Migration, , . .

+11

Realm 0.84.2 , (0.84.2) , :

  • version 0, db . , , schemaVersion , .

  • The schemaVersion scheme is automatically saved, and when a new installation of your application occurs, and you are already on scheme 3, the area automatically checks if there are exceptions if it does not set schemaVersion to 3, so your migrations are not performed when it is not needed. It also means that you no longer need to store anything in SharedPreferences.

  • During the migration process, you must set all the values ​​of the new columns when the type is not null, ...

  • Empty rows can be inserted, but only when setting convertColumnToNullable in a column

+1
source

All Articles