EyeQ Tech
EyeQ Tech

Reputation: 7378

realm, check schema version

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.

Upvotes: 2

Views: 3148

Answers (2)

Sachini Samarasinghe
Sachini Samarasinghe

Reputation: 1111

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();

Upvotes: 6

Ralphilius
Ralphilius

Reputation: 1856

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.

Upvotes: 0

Related Questions