我有一个迁移文件,如下所示: -
public class Migration implements RealmMigration {
@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
if(oldVersion == 1){
RealmObjectSchema replyDBschema = schema.create("ReplyDB");
replyDBschema.addField("date",String.class, FieldAttribute.PRIMARY_KEY);
replyDBschema.addField("id",int.class);
replyDBschema.addField("conversationId",String.class);
replyDBschema.addField("replyFrom",String.class);
replyDBschema.addField("reply",String.class);
RealmObjectSchema conversationDBschema = schema.create("ConversationDB");
conversationDBschema.addField("conversationId",String.class, FieldAttribute.PRIMARY_KEY);
conversationDBschema.addField("id",String.class);
conversationDBschema.addField("parentId",int.class);
conversationDBschema.addField("topic",String.class);
conversationDBschema.addField("date",String.class);
conversationDBschema.addField("typeOfConversation",String.class);
conversationDBschema.addField("studentId",int.class);
conversationDBschema.addRealmListField("replyDBList", schema.get("ReplyDB"));
if (!schema.get("ActivityDB").hasField("conversationDB")) {
schema.get("ActivityDB").addRealmObjectField("conversationDB", conversationDBschema);
}
//schema.get("ActivityDB").addRealmObjectField("conversationDB",schema.get("ConversationDB"));
oldVersion++;
}
}
}
因为我添加了两个新的数据库文件,即ReplyDB.class和ConversationDB.class。 我还编辑了ActivityDB以在其中包含ConversationDB对象。 对于所有这些,我创建了上面的迁移文件。
我从以下Realm文件中调用迁移文件
public class RealmApplication extends CalligraphyApplication {
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
final RealmConfiguration configuration = new RealmConfiguration.Builder().name("parents.realm").schemaVersion(2).migration(new Migration()).build();
Realm.setDefaultConfiguration(configuration);
Realm.getInstance(configuration);
}
@Override
public void onTerminate() {
Realm.getDefaultInstance().close();
super.onTerminate();
}
}
此外,我已经使用调试器进行了测试,调用了域迁移。
然而,应用程序崩溃时抛出以下错误: -
Caused by: io.realm.exceptions.RealmMigrationNeededException: Missing field 'conversationDB' in existing Realm file. Either remove field or migrate using io.realm.internal.Table.addColumn().
at io.realm.ActivityDBRealmProxy.validateTable(ActivityDBRealmProxy.java:814)
at io.realm.DefaultRealmModuleMediator.validateTable(DefaultRealmModuleMediator.java:85)
at io.realm.Realm.initializeRealm(Realm.java:480)
at io.realm.Realm.createAndValidateFromCache(Realm.java:423)
at io.realm.Realm.createInstance(Realm.java:387)
at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:346)
at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:284)
at io.realm.Realm.getInstance(Realm.java:301)
at ai.edbox.parents.utils.RealmApplication.onCreate(RealmApplication.java:18)
at ai.edbox.parents.utils.FabricApplication.onCreate(FabricApplication.java:14)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1024)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5403)
at android.app.ActivityThread.-wrap2(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
请帮我解决此错误。谢谢。