"Realm Migration Needed", Exception in Android While Retrieving Values from Realm Db

realm migration needed , exception in android while retrieving values from realm db

EDIT: for new versions of Realm, Realm.init(Context context) is added

Realm.init(context);    
RealmConfiguration config = new RealmConfiguration
.Builder()
.deleteRealmIfMigrationNeeded()
.build();

NOTE: With this config option, any schema change will result in loss of data, specifically:

  • a field is added/removed
  • a new RealmObject class is added
  • an existing RealmObject is removed
  • @Required is added/removed
  • @PrimaryKey is added/removed
  • @Index is added/removed

So it's primarily recommended while the app is in the development stage.


Or add a migration following the official docs:

https://realm.io/docs/java/latest/#migrations

For example,

public class Migration implements RealmMigration {
@Override
public void migrate(final DynamicRealm realm, long oldVersion, long newVersion) {
RealmSchema schema = realm.getSchema();

if (oldVersion == 0) {
RealmObjectSchema personSchema = schema.get("Person");
personSchema
.addField("fullName", String.class, FieldAttribute.REQUIRED);
oldVersion++;
...

// hash code, equals

And

Realm.init(context);    
RealmConfiguration config = new RealmConfiguration.Builder()
.migration(new Migration())
// .deleteRealmIfMigrationNeeded()
.build();

RealmMigrationNeededException: Migration is required Realm, while adding new table on existing database, Android

     if(oldVersion == 2){
schema.create("History")
.addField(DBHelper.COLUMN_NAME_ID, int.class)
.addField(DBHelper.COLUMN_NAME_WORD, String.class, FieldAttribute.REQUIRED)
.addField(DBHelper.COLUMN_NAME_OBJECT_ID, String.class, FieldAttribute.REQUIRED)
.addField(DBHelper.COLUMN_NAME_PRONOUCE, String.class, FieldAttribute.REQUIRED)
.addField(DBHelper.COLUMN_NAME_TYPE, String.class, FieldAttribute.REQUIRED)
.addField(DBHelper.COLUMN_NAME_MEANING, String.class, FieldAttribute.REQUIRED);
oldVersion++;
}

On your test phone, just uninstall/reinstall the app afterwards, or you'll need to bump the schema version and change the fields to the right type.

RealmMigrationNeededException when changing Realm model

You should be able to find the information you need here:

https://realm.io/docs/java/latest/#migrations

Just changing your code to the new definition will work fine, if you
have no data stored on disk under the old database schema. But if you
do, there will be a mismatch between what Realm sees defined in code &
the data Realm sees on disk, so an exception will be thrown.

Can't avoid io.realm.exceptions.RealmMigrationNeededException

You need to call Realm.setDefaultConfiguration(config) after creating said config.

public class BaseApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
RealmConfiguration config = new RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.build()
Realm.setDefaultConfiguration(config); // <---
}
}

Realm Migration Issues

The problem is that in other parts of the code you are calling Realm.getInstance(context).

This is equivalent to doing the following:

RealmConfiguration config = new RealmConfiguration.Builder(context).schemaVersion(0).build();
Realm realm = Realm.getInstance(config);

Which will give you the schema mismatch error you are seeing.

You can read more about default configurations here: https://realm.io/docs/java/latest/#the-default-realm

Realm not auto-deleting database if migration needed

We had a similar issue. We solved this by adding

Realm.getInstance(config)

right after

Realm.setDefaultConfiguration(config);

We think the configuration will be set up after Realm is called the first time. This time we don't use any Realm object so there's no exception.

What happens to Realm database when the user updates the Android app from the store?

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


Related Topics



Leave a reply



Submit