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

亡梦爱人 提交于 2019-11-26 04:43:48

问题


I am using Realm as a back end in my application. I have created one table named Setting. I added values in that table, by following the steps given on Realm\'s official site. But when I am going to retrieve values from that table in, I getting exception

\"io.realm.exceptions.RealmMigrationNeededException: RealmMigration must be provided\" on the line:\" realm=Realm.getInstance(getApplicationContext());\".

Actually, I am new to android and Realm, so finding trouble to understand what is problem.


回答1:


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



回答2:


if you upload the app to store, the "delete and reinstall the app" will not working to other user, so you must working with "deleting" the realm and "reinstalling" the realm, not the app. here's the way to do it, hope it'll helps!!

    RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).build();

    try {
        return Realm.getInstance(realmConfiguration);
    } catch (RealmMigrationNeededException e){
        try {
            Realm.deleteRealm(realmConfiguration);
            //Realm file has been deleted.
            return Realm.getInstance(realmConfiguration);
        } catch (Exception ex){
            throw ex;
            //No Realm file to remove.
        }
    }

EDIT

For the newest Realm (3.0.0), Realm has changes the constructor structure, so you must do something like this :

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



回答3:


You changed something to the realm structure.

In order to fix it you should include the migration or simply remove the application and install it again.




回答4:


That works for me

    Realm.init(context);
    Realm realm;
    try{
        realm = Realm.getDefaultInstance();

    }catch (Exception e){

        // Get a Realm instance for this thread
        RealmConfiguration config = new RealmConfiguration.Builder()
                .deleteRealmIfMigrationNeeded()
                .build();
        realm = Realm.getInstance(config);

    }



回答5:


Kotlin version:

val realm = try {
            Realm.init(this)
            val config = RealmConfiguration.Builder()
                    .deleteRealmIfMigrationNeeded()
                    .build()
            Realm.getInstance(config)
        } catch (ex: RealmMigrationNeededException) {
            Realm.getDefaultInstance()
        }


来源:https://stackoverflow.com/questions/33940233/realm-migration-needed-exception-in-android-while-retrieving-values-from-real

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!