我正在使用等级版本:编译' io.realm:realm-android:0.87.5'。
在我的应用程序文件中,我编写了如下代码
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
.schemaVersion(2)
.migration(new Migration())
.build();
Realm.setDefaultConfiguration(realmConfiguration);
在我的活动/片段中,我按如下方式打开领域实例
@Override
public void onStart(){
super.onStart();
realm = Realm.getDefaultInstance();
}
@Override
public void onStop(){
super.onStop();
realm.close();
}
但我总是遇到如下例外
03-10 14:55:32.385 7015-7015/com.workoutguru.workoutcash E/AndroidRuntime: FATAL EXCEPTION: main
03-10 14:55:32.385 7015-7015/com.workoutguru.workoutcash E/AndroidRuntime: Process: com.workoutguru.workoutcash, PID: 7015
03-10 14:55:32.385 7015-7015/com.workoutguru.workoutcash E/AndroidRuntime: java.lang.IllegalArgumentException: Configurations cannot be different if used to open the same file.
03-10 14:55:32.385 7015-7015/com.workoutguru.workoutcash E/AndroidRuntime: Cached configuration:
03-10 14:55:32.385 7015-7015/com.workoutguru.workoutcash E/AndroidRuntime: realmFolder: /data/data/com.workoutguru.workoutcash/files
03-10 14:55:32.385 7015-7015/com.workoutguru.workoutcash E/AndroidRuntime: realmFileName : default.realm
03-10 14:55:32.385 7015-7015/com.workoutguru.workoutcash E/AndroidRuntime: canonicalPath: /data/data/com.workoutguru.workoutcash/files/default.realm
03-10 14:55:32.385 7015-7015/com.workoutguru.workoutcash E/AndroidRuntime: key: [length: 0]
E/AndroidRuntime: schemaVersion: 2
E/AndroidRuntime: migration: E/AndroidRuntime:
durability: FULL
schemaMediator: io.realm.DefaultRealmModuleMediator@a3db7d18
我的迁移文件代码如下
public class Migration implements RealmMigration {
@Override
public void migrate(final DynamicRealm realm, long oldVersion, long newVersion){
RealmSchema schema = realm.getSchema();
if(oldVersion == 1){
RealmObjectSchema sweatPointSchema = schema.get("UserInfoDB");
sweatPointSchema.addField("sweatPoints",Integer.class);
oldVersion++;
}
if(oldVersion == 2){
RealmObjectSchema teamInfoSchema = schema.get("TeamInfoDB");
teamInfoSchema.addField("teamDescription",String.class);
oldVersion ++;
}
}
}