Felix Yah Batta Man
Felix Yah Batta Man

Reputation: 459

Open realm with new realmconfiguration

I used Realm in my android untill now with new RealmConfiguration.Builder(this) .build();

I just read later about the possibility to add schema and migration. So in my new version for my app i want to add the migration feature. so i changed the line above to:

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

but now I get the error

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

How can i change the configuration without deleting the database

Upvotes: 12

Views: 6211

Answers (2)

Christian Melchior
Christian Melchior

Reputation: 20126

I think your problem is that you are creating your RealmConfiguration multiple times. That shouldn't be a problem by itself (although it is inefficient), but the problem arises with your Migration class. Internally we compare all state in the configuration objects and if you didn't override equals and hashCode in your Migration class you have a case where new Migration().equals(new Migration()) == false which will give you the error you are seeing.

One solution is adding this:

public class Migration implements RealmMigration {

  // Migration logic...

  @Override
  public int hashCode() {
    return 37;
  }

  @Override
  public boolean equals(Object o) {
    return (o instanceof Migration);
  }
}

Upvotes: 34

Sung-Taek Kim
Sung-Taek Kim

Reputation: 69

When you set a new schema version with schemaVersion(), the version number should equal to or higher than the schema version of the existing realm file. The RealmMigration() you provide should then be able to convert older version of schemas to the new version.

I'd suggest to check your existing schema version first, then check your RealmObjects for appropreate conversion.

Upvotes: 0

Related Questions