我是Realm Android的新用户,目前我需要在我的项目中使用Realm,但我经常遇到这个Cannot migrate a Realm file that is already open
错误,这种情况经常在我打开应用程序时发生。
FATAL EXCEPTION: main
Process: com.etobee.driver, PID: 31866
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.etobee.driver/com.etobee.driver.activity.HomeActivity}: java.lang.IllegalStateException: Cannot migrate a Realm file that is already open: /data/data/com.etobee.driver/files/default6
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2460)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522)
at android.app.ActivityThread.access$800(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5546)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:967)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
Caused by: java.lang.IllegalStateException: Cannot migrate a Realm file that is already open: /data/data/com.etobee.driver/files/default6
at io.realm.BaseRealm$3.onResult(BaseRealm.java:740)
at io.realm.RealmCache.invokeWithGlobalRefCount(RealmCache.java:274)
at io.realm.BaseRealm.migrateRealm(BaseRealm.java:735)
at io.realm.Realm.migrateRealm(Realm.java:1361)
at com.etobee.driver.util.RealMHelper.init(RealMHelper.java:35)
at com.etobee.driver.activity.DefaultActivity.onCreate(DefaultActivity.java:138)
at com.etobee.driver.activity.HomeActivity.onCreate(HomeActivity.java:91)
at android.app.Activity.performCreate(Activity.java:5975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522)
at android.app.ActivityThread.access$800(ActivityThread.java:169)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5546)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
以下是我初始化Realm的方法,我在主要活动中将其称为onCreate
。
public static void init(Context context){
// Create a RealmConfiguration that saves the Realm file in the app's "files" directory.
RealmConfiguration config0 = new RealmConfiguration.Builder(context)
.name("default6")
.schemaVersion(7)
.build();
try {
Realm.migrateRealm(config0, new RealmMigration() { /**--- ERROR HERE **/
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
// DynamicRealm exposes an editable schema
RealmSchema schema = realm.getSchema();
}
});
} catch (FileNotFoundException ignored) {
// If the Realm file doesn't exist, just ignore.
}
Realm.setDefaultConfiguration(config0);
}
事情是它并不总是发生,它是随机发生的,但经常发生。我已经搜索过这个问题,但还没有找到。 请帮忙。
提前谢谢。
答案 0 :(得分:1)
static RealmConfiguration realmConfiguration = null;
public static void init(Context context){
if(realmConfiguration == null) {
// Create a RealmConfiguration that saves the Realm file in the app's "files" directory.
realmConfiguration = new RealmConfiguration.Builder(context.getApplicationContext())
.name("default6")
.schemaVersion(7)
.migration(new MyMigration())
.build();
Realm.setDefaultConfiguration(realmConfiguration);
}
}
和
public class MyMigration implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
// DynamicRealm exposes an editable schema
RealmSchema schema = realm.getSchema();
// Loop through each version when an upgrade occurs.
for (long version = oldVersion + 1; version <= newVersion; version++) {
switch (version) {
case 1:
// migrate to version 1
break;
case 2:
// migrate to version 2
break;
//...
case 7:
// migrate to version 7
break;
}
}
}
@Override
public int hashCode() {
return MyMigration.class.hashCode();
}
@Override
public boolean equals(Object obj) {
return obj != null && obj instanceof MyMigration;
}
}