Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm.getInstance(context) not working anymore after Migration

Tags:

android

realm

After doing a migration, i cant use the Realm.getInstance(context); anymore

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

i saw that i must use the created configs previosly to do the migration

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

now i must create the config1 again everywhere i use the

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 i must create this CONFIG1 (RealmConfiguration) EVERYWHERE? or is there any other approach?

i tried adding to the MainClass this Realm.setDefaultConfiguration(CONFIG1); but no success =/

i was wrong for checking or avoiding null on realm object calling the getInstance for each method?

like image 661
user2582318 Avatar asked Mar 15 '23 02:03

user2582318


1 Answers

Use Realm.setDefaultConfiguration(CONFIG1); to set default configuration :

and use Realm.getDefaultInstance(); to get default realm instance.

like image 155
arbrcr Avatar answered Mar 16 '23 16:03

arbrcr