I would like to know what happens with Realm when the user updates the Android app. I have an Android application with some classes that extend from RealmObject. I use them to persist information on my app. The thing is, when I define new classes as RealmObject, and I run the app directly from Android Studio, the app crashes: The MyRealmClass class is missing from the schema for this Realm. I have to uninstall the previous version and then run the app to solve it.
What happens when the user updates the app from Play Store and the new version has new Realm classes? Would the app crash? If so, there is any way to solve it?
Thank you!
Yes, the app would crash. You need to add a RealmMigration class.
public class CustomMigration implements RealmMigration {
@Override
public long migrate(DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();
if (oldVersion == 0) {
// Migrate from v0 to v1
schema.create("myNewTable"); // example
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));
}
}
}
and
RealmConfiguration config = new RealmConfiguration.Builder(context)
.schemaVersion(2)
.migration(new MyMigration())
.build();
Realm.setDefaultConfiguration(config);
// This will automatically trigger the migration if needed
Realm realm = Realm.getDefaultInstance();