2016-07-31 7 views
2

マイグレーションを適用するレルムモデルがあります。私はいくつかの値を取得するには、レルムのインスタンスを使用しレルムはマイグレーションできません

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

:私はマイグレーションを適用する場合しかし、私は私のActivityクラスでエラー

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にフィールドとしてスキーマのバージョンを追加し、オーバーライドのequals():

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

オーバーライドイコールと以下のようにMigrationクラスのhashcodeメソッドを使用します。

@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

私はこの問題がメッセージの最初の部分だと思っています "同じファイルを開くために使用すると設定が異なることはありません。"レルムを開くために2つの異なる設定を使用しています。あなたの例の1つはschemaVersion 0を使用し、もう1つはschemaVersion 1を使用します。アプリケーション全体で同じバージョンを使用する必要があります。

新しいデータ移行が必要になるたびに、スキーマのバージョン番号をバンプし、古い/新しいスキーマのバージョンを調べ、適切な移行を行うコードをクラスMigrationに追加します。

関連する問題