我在原来的realm数据库上新增一个PageData2表然后配置了migrate,但是在调用getDefaultInstance方法的时候还是报错
java.lang.RuntimeException: Unable to pause activity {tv.mchang.iptv/tv.mchang.app.SplashActivity}: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
- Class 'PageDate2' has been added.
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3048)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3003)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:2981)
at android.app.ActivityThread.access$1000(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1213)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:812)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
at dalvik.system.NativeStart.main(Native Method)
Caused by: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
- Class 'PageDate2' has been added.
at io.realm.internal.OsSharedRealm.nativeGetSharedRealm(Native Method)
at io.realm.internal.OsSharedRealm.(OsSharedRealm.java:171)
at io.realm.internal.OsSharedRealm.getInstance(OsSharedRealm.java:241)
at io.realm.BaseRealm.(BaseRealm.java:136)
at io.realm.BaseRealm.(BaseRealm.java:105)
at io.realm.Realm.(Realm.java:164)
at io.realm.Realm.createInstance(Realm.java:435)
at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:342)
at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:282)
at io.realm.Realm.getDefaultInstance(Realm.java:343)
at tv.mchang.data.realm.statistics.StatisticsDataUtils.addPageData(StatisticsDataUtils.java:17)
at tv.mchang.app.SplashActivity.saveStatisticsData(SplashActivity.java:216)
at tv.mchang.app.SplashActivity.onPause(SplashActivity.java:211)
at android.app.Activity.performPause(Activity.java:5351)
at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1233)
at com.odin.framework.hack.InstrumentationHook.callActivityOnPause(InstrumentationHook.java:352)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3034)
... 12 more
具体代码如下:
在Application初始化了Realm并设置了setDefaultConfiguration,原来schemaVersion为8,我改为9
Realm.init(this);
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
.directory(realmDir)
.name("DataCenter.realm")
.schemaVersion(9)
.migration(new McMigration())
.build();
Realm.setDefaultConfiguration(realmConfiguration);
然后在migrate新增
if (oldVersion == 8) {
realmSchema.create("PageData2")
.addField("pageName", String.class)
.addField("stayTime", long.class)
.addField("visitTimes", int.class);
}
原本每次新增的时候应该在结尾oldVersion++的,但是之前维护人员在版本为5之后就没有oldVersion++了,我应该怎么修复?具体情况如下:
if (oldVersion == 5) {
realmSchema.get("OpusPublishInfo")
.renameField("opusId", "publishId")
.addField("mcId", long.class)
.addField("songName", String.class)
.addField("singer", String.class);
oldVersion++;
}
if (oldVersion == 6) {
realmSchema.get("VisitData")
.removeField("visitTimes")
.addField("visitTime", long.class);
realmSchema.create("OrderEvent")
.addField("videoId", String.class)
.addField("videoType", String.class)
.addField("eventTime", long.class);
}
if (oldVersion == 7) {
realmSchema.remove("AccountInfo");
realmSchema.remove("McUserInfo");
}
if (oldVersion == 8) {
realmSchema.create("PageData2")
.addField("pageName", String.class)
.addField("stayTime", long.class)
.addField("visitTimes", int.class);
}
然后新建类:
public class PageDate2 extends RealmObject {
String pageName;
long stayTime;
int visitTimes;
public PageDate2(){
}
public PageDate2(String pageName,long stayTime,int visitTimes){
this.pageName = pageName;
this.stayTime = stayTime;
this.visitTimes = visitTimes;
}
最后调用:
public static void addPageData(String pageName, long stayTimeMills) {
try (Realm realm = Realm.getDefaultInstance()) {
PageDate2 pageDate2 = new PageDate2(pageName,stayTimeMills,1);
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm1) {
realm1.copyToRealmOrUpdate(pageDate2);
}
});
}
}
报错!请问我在哪里出了问题?应该如何改正呢?求大佬指教!我上网查了很多但是还是找不到解决办法。