2016-07-31 6 views
2

마이그레이션을 적용 할 영역 모델이 있습니다. 나는 어떤 값을 얻을 수있는 영역 인스턴스를 사용영역을 마이그레이션 할 수 없습니다.

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

: 나는 마이그레이션을 적용 할 때 그러나, 나는 내 활동 클래스에서 오류

Configurations cannot be different if used to open the same file. 
The most likely cause is that equals() and hashCode() are not overridden in the migration class: 

를 얻을로 구성이 설정됩니다.

RealmConfiguration config = new RealmConfiguration.Builder(this) 
      .schemaVersion(1) // Must be bumped when the schema changes 
      .migration(new Migration()) // Migration to run 
      .build(); 

Realm.setDefaultConfiguration(config); 

나는이 전화

: realm = Realm.getDefaultInstance(); 내가 오류 위 얻을 그리고 내가 사용하여 마이그레이션을 적용합니다. 마이그레이션을 올바르게 적용하고 있습니까?

답변

1

Migration 클래스에서 equalshashcode을 무시하려고 시도 했습니까? 예외 메시지에서 말하는대로?

The most likely cause is that equals() and hashCode() are not overridden in the migration class 
-1

MyMigration에 필드로 스키마 버전을 추가하고 무시 등호() :

private final int version; 

    @Override 
    public boolean equals(Object o) { 
     return this.version == ((MyMigration)o).version; 
    } 

    public MyMigration(int version) { 
     this.version = version; 
    } 
1

과 같아야 마이그레이션 :

public class MyMigration implements Migration { 
    //... migration 

    public int hashCode() { 
     return MyMigration.class.hashCode(); 
    } 

    public boolean equals(Object object) { 
     if(object == null) { 
      return false; 
     } 
     return object instanceof MyMigration; 
    } 
} 
-1

재정의 등호 및 해시 코드 메소드를 다음과 같이 마이그레이션 클래스에 추가하십시오.

@Override 
public boolean equals(Object obj) { 
    return obj != null && obj instanceof Migration; // obj instance of your Migration class name, here My class is Migration. 
} 

@Override 
public int hashCode() { 
    return Migration.class.hashCode(); 
} 
0

나는이 문제가 메시지의 첫 번째 부분이라고 생각한다. "동일한 파일을 여는 데 사용하면 구성이 다를 수 없습니다." 영역을 여는 데 두 가지 다른 구성을 사용하고 있습니다. 귀하의 예제 중 하나는 schemaVersion 0을 사용하고 다른 하나는 schemaVersion 1을 사용합니다. 앱 전체에서 동일한 버전을 사용해야합니다.

새 데이터 마이그레이션이 필요할 때마다 스키마 버전 번호를 높이고 이전/새 스키마 버전을보고 적절한 마이그레이션을 수행하는 마이그레이션 클래스에 코드를 추가하십시오.

관련 문제