realm, check schema version

江枫思渺然 提交于 2020-02-04 10:18:32

问题


I'm using Realm for Android, I'm wondering how to get the current schema version. In their Migration example, the version is incremented then saved, but I need to check the version before doing any upgrade. Thanks.


回答1:


You can use the below code to get the current schema version of Realm before migration.

Realm.init(context);
RealmConfiguration realmConfig = new RealmConfiguration.Builder().build();
DynamicRealm realm = DynamicRealm.getInstance(realmConfig);
long version = dynRealm.getVersion();//this will return the existing schema version
dynRealm.close();



回答2:


In the Migration class from the example, you can see that they check for every version. The version parameter is the current version of the current realm. If you notice that after every if statement, the version is incremented.

if (version == desiredVersion){
   //Migration script
   version++;
}

In any way, you will need to write migration scripts for all versions whose changes need migration. Otherwise, the app will be crashed.



来源:https://stackoverflow.com/questions/31419803/realm-check-schema-version

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!