Realm.getInstance (context) no longer works after migration

After the migration, I no longer use Realm.getInstance(context);

Mistake: java.lang.IllegalArgumentException: Configurations cannot have different schema versions if used to open the same file. 1 vs. 0

I saw that I should use the created configurations earlier in order to migrate

RealmConfiguration config1 = new RealmConfiguration.Builder(this)
                .schemaVersion(1)
                .migration(new Migration())
                .build();

now i have to create config1 again wherever i use

Realm.getInstance(context);

I used to add this for each method to avoid any kind of null

public List<CustomClass> getAll() {
realm = Realm.getInstance(context);
....
}



public CustomClass getOneById(int id) {
realm = Realm.getInstance(context);
...
}

and sometimes outside the DAO class, where I dynamically change the database, I use this:

Realm.getInstance(context).beginTransaction();

change object

Realm.getInstance(context).commitTransaction();

Now should I create this CONFIG1 (RealmConfiguration) EVERYWHERE? or is there a different approach?

I tried to add this to MainClass Realm.setDefaultConfiguration(CONFIG1);, but did not have time = /

i was wrong to check or null for a realm object calling getInstance for each method?

+4
1

Realm.setDefaultConfiguration(CONFIG1);   :

Realm.getDefaultInstance();, .

+6

Source: https://habr.com/ru/post/1617131/


All Articles