Open area with new realmconfiguration

I have used Realm in my android so far with new RealmConfiguration.Builder(this) .build();

I just read later about the possibility of adding a schema and migration. Therefore, in my new version for my application, I want to add a transfer function. so I changed the line above:

 new RealmConfiguration.Builder(this) .schemaVersion(0) .migration(new Migration()) .build(); 

but now i get an error

 IllegalArgumentException: Configurations cannot be different if used to open the same file. 

How can I change the configuration without deleting the database

+6
source share
2 answers

I think your problem is that you create your RealmConfiguration several times. This should not be a problem in itself (although it is inefficient), but the problem arises with your Migration class. Inside, we compare all the states in the configuration objects, and if you did not redefine equals and hashCode in the Migration class, you have a case where new Migration().equals(new Migration()) == false , which will give you the error that you you see.

The following is added to one solution:

 public class Migration implements RealmMigration { // Migration logic... @Override public int hashCode() { return 37; } @Override public boolean equals(Object o) { return (o instanceof Migration); } } 
+19
source

When you install a new version of a schema with schemaVersion() , the version number must be equal to or greater than the version of the schema of the existing realm file. Then, the RealmMigration() that you provide can convert the older version of the schemas to the new version.

I would suggest checking your existing version of the schema first and then checking RealmObject for the appropriate conversion.

0
source

All Articles