Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open realm with new realmconfiguration

Tags:

android

realm

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

like image 657
Felix Yah Batta Man Avatar asked Apr 28 '16 06:04

Felix Yah Batta Man


1 Answers

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);
  }
}
like image 188
Christian Melchior Avatar answered Nov 07 '22 17:11

Christian Melchior