Realm報錯RealmMigrationNeededException: Migration is required due to the following errors

報錯信息:

io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:
    - Property 'CommonMediaInfo.traceId' has been added.
    - Property 'CommonMediaInfo.traceInfo' has been added.
    - Property 'CommonMediaInfo.sceneId' has been added.
        at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3381)
        at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3340)
        at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3315)
        at android.app.ActivityThread.-wrap13(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5422)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

解決方法:

public class MyMigration implements RealmMigration {
    @Override
    public void migrate(@NonNull DynamicRealm realm, long oldVersion, long newVersion) {
        if (oldVersion == 12) {    
            Objects.requireNonNull(realmSchema.get("CommonMediaInfo")) 

                .addField("traceId", String.class)            
                .addField("traceInfo", String.class)            
                .addField("sceneId", String.class);    
            oldVersion++;
        }
    }
}

發佈了7 篇原創文章 · 獲贊 0 · 訪問量 2320
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章