Interface RealmMigration
On this page
io.realm
The RealmMigration class is used to perform the migration of one Realm schema to another. The schema for a Realm is defined by all classes in a project that extend io.realm.RealmObject or implement io.realm.RealmModel , 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 { public long migrate(DynamicRealm realm, long oldVersion, long newVersion) { RealmSchema schema = realm.getSchema(); if (oldVersion == 0) { // Migrate from v0 to v1 oldVersion++; } if (oldVersion == 1) { // Migrate from v1 to v2 oldVersion++; } if (oldVersion < newVersion) { throw new IllegalStateException(String.format(Locale.US, "Migration missing from v%d to v%d", oldVersion, newVersion)); } } }
During development when RealmObject classes can change frequently, it is possible to use io.realm.Realm.deleteRealm(RealmConfiguration) . This will delete the database file and eliminate the need for any migrations.
Method Summary
Modifier and Type | Method and Description |
---|---|
public void | This method will be called if a migration is needed. |
Method Detail
migrate
This method will be called if a migration is needed. The entire method is wrapped in a write transaction so it is possible to create, update or delete any existing objects without wrapping it in your own transaction. Parameters
|