The RealmMigration class is used to describe the migration of one Realm schema to another.
The schema for a Realm is defined by all classes in a project that extend
RealmObject
, so any changes to these classes will require a migration.
To support migrations from any previous schemaVersion to the newest, the following pattern is
recommended when writing a migration:
public class CustomMigration implements RealmMigration {
\@Override
public long execute(Realm realm, long schemaVersion) {
if (schemaVersion == 0) {
// Migrate from v0 to v1
schemaVersion++;
}
if (schemaVersion == 0) {
// Migrate from v0 to v1
schemaVersion++;
}
return schemaVersion;
}
}
During development when model classes can change frequently, it is possible to use
Realm.deleteRealmFile(android.content.Context)
. This will delete the database
file and eliminate the need for any migrations.