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
Using Resultreceiver in Android
Redraw a Single Row in a Listview
Reading SQLite File from Asset Folder
Launch Activities from Different Package
What Is the Class R in Android
How to Determine If One of My Activities Is in the Foreground
Force Update of an Android App When a New Version Is Available
Recyclerview Inside Scrollview Not Scrolling Smoothly
Androidstudio Cannot Find Layout
Complete Working Sample of the Gmail Three-Fragment Animation Scenario
What Is the Purpose of Android's <Merge> Tag in Xml Layouts
How to Lock Orientation During Runtime
How to Make a View with Rounded Corners
Toolbar and Contextual Actionbar with Appcompat-V7
Best Practice for Nested Fragments in Android 4.0, 4.1 (<4.2) Without Using the Support Library