2016-04-01 14 views
0

マイグレーションがうまく動作しますが、ある時点で私のアプリはインターフェイスの処理を再開する必要があります(recreate())。なぜなら、移行のこの時点で、アプリのクラッシュ:再作成()メソッドの後に移行がクラッシュする

java.lang.RuntimeException: Unable to start activity ComponentInfo{APP/APP.MainActivity}: java.lang.IllegalArgumentException: Configurations cannot be different if used to open the same file. 
              Cached configuration: 
              realmFolder: /data/user/0/APP/files 
              realmFileName : default.realm 
              canonicalPath: /data/data/APP/files/default.realm 
              key: [length: 0] 
              schemaVersion: 0 
              migration: null 
              deleteRealmIfMigrationNeeded: false 
              durability: FULL 
              schemaMediator: [email protected] 

の移行は、スニペット:

mRealmConfig = new RealmConfiguration.Builder(this) 
     .schemaVersion(0) 
     .migration(new Migration()) 
     .build(); 
realm = Realm.getInstance(mRealmConfig); 
realm.close(); 

移行クラス:あなたは複数回RealmConfigurationインスタンスを作成しているよう

public class Migration implements RealmMigration { 
@Override 
public void migrate(final DynamicRealm realm, long oldVersion, long newVersion) { 
    RealmSchema schema = realm.getSchema(); 

    // Migrate from version 0 to version 1 
    if (oldVersion == 0) { 
     RealmObjectSchema notificationSchema = schema.get("classTarget"); 

     notificationSchema 
       .addField("something", Boolean.class, FieldAttribute.REQUIRED) 
       .transform(new RealmObjectSchema.Function() { 
        @Override 
        public void apply(DynamicRealmObject obj) { 
         obj.set("something", true); 
        } 
       }); 
     oldVersion++; 
    } 
} 
} 

答えて

1

それは聞こえるが。静的なフィールドに保持するか、代わりにRealm.setDefaultInstance(config)/Realm.getDefaultInstance()を使用してください。

RealmMigrationクラスがequals()/hashCode()をオーバーライドしないため、例外が表示される理由があります。つまり、内部チェックでは2つの異なる設定があると考えられます。new Migration().equals(new Migration()) == false

+0

マイグレーションを正しく開始するには、スキーマバージョンを0からバンプする必要があります。 –

関連する問題