3

i can't use migration realm

in my project, you need to configure the migration of realm. I created a migration class,I added migration and added field to realmObject but don't work migration

i get this error message :

Migration is required due to the following errors:

  • Property 'PickupState.latitude' has been added.
  • Property 'PickupState.longitude' has been added.

old realmObject model

public class PickupState extends RealmObject {
  @PrimaryKey
  private long autoIncrementId;
  private long id;
  private String userName;
  private String eventType;
  private String eventData;
  private String syncStatus;
  private String syncErrorCode;

  public void setAutoIncrementId(long l) {
    autoIncrementId = l;
  }

  public long getAutoIncrementId() {
    return autoIncrementId;
  }

  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getEventType() {
    return eventType;
  }

  public void setEventType(State eventType) {
    this.eventType = eventType.name();
  }

  public String getEventData() {
    return eventData;
  }

  public void setEventData(String eventData) {
    this.eventData = eventData;
  }

  public String getSyncStatus() {
    return syncStatus;
  }

  public void setSyncStatus(String syncStatus) {
    this.syncStatus = syncStatus;
  }

  public String getSyncErrorCode() {
    return syncErrorCode;
  }

  public void setSyncErrorCode(String syncErrorCode) {
    this.syncErrorCode = syncErrorCode;
  }
}

new realmObject

public class PickupState extends RealmObject {
  @PrimaryKey
  private long autoIncrementId;
  private long id;
  private String userName;
  private String eventType;
  private String eventData;
  private String syncStatus;
  private String syncErrorCode;
  private double latitude;
  private double longitude;

  public void setAutoIncrementId(long l) {
    autoIncrementId = l;
  }

  public long getAutoIncrementId() {
    return autoIncrementId;
  }

  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  public String getUserName() {
    return userName;
  }

  public void setUserName(String userName) {
    this.userName = userName;
  }

  public String getEventType() {
    return eventType;
  }

  public void setEventType(State eventType) {
    this.eventType = eventType.name();
  }

  public String getEventData() {
    return eventData;
  }

  public void setEventData(String eventData) {
    this.eventData = eventData;
  }

  public String getSyncStatus() {
    return syncStatus;
  }

  public void setSyncStatus(String syncStatus) {
    this.syncStatus = syncStatus;
  }

  public String getSyncErrorCode() {
    return syncErrorCode;
  }

  public void setSyncErrorCode(String syncErrorCode) {
    this.syncErrorCode = syncErrorCode;
  }

  public double getLatitude() {
    return latitude;
  }

  public void setLatitude(double latitude) {
   this.latitude = latitude;
    }

   public double getLongitude() {
    return longitude;
  }

   public void setLongitude(double longitude) {
   this.longitude = longitude;
  }
}

migration class:

public class PickupStateMigration implements RealmMigration {

  @Override
  public void migrate(@NonNull DynamicRealm realm, long oldVersion, long newVersion) {
    final RealmObjectSchema schema = realm.getSchema().get("PickupState");
    assert schema != null;
    if (oldVersion == 0) {
        schema.addField("latitude", double.class);
        schema.addField("longitude", double.class);
      oldVersion++;
    }
  }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Kourosh
  • 2,239
  • 13
  • 18

2 Answers2

2

For the migration to be executed, you also need to bump the schema version.

RealmConfiguration config = new RealmConfiguration.Builder()
                                    .schemaVersion(1)
                                    .migration(new PickupStateMigration())
                                    // ...
                                    .build()
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • I did it, but that's not the problem – Kourosh Jul 04 '18 at 12:12
  • There is no other way this can go wrong. You are probably calling `Realm.getDefaultInstance()` without having set the `Realm.setDefaultConfiguration()` so you are loading the Realm without the migration in its configuration. – EpicPandaForce Jul 04 '18 at 12:42
0

I made two table and two different schemaVersion But I changed one version and migration Now I've changed two versions and I've got same migration for them

I must used module for each table but I didn't do it

Kourosh
  • 2,239
  • 13
  • 18