Realm Error io.realm.exceptions.RealmMigrationNeededException on Android

0

I am starting to use database style Realm after adding a new field Age to class Person

import io.realm.RealmObject;
import io.realm.annotations.Index;
import io.realm.annotations.PrimaryKey;

public class Person extends RealmObject {
    @PrimaryKey
    private long id;

    @Index
    private String name;

    private int age;

    public long getId() { return id; }

    public void setId(long id) { this.id = id; }

    public String getName() { return name; }

    public void setName(String name) { this.name = name; }

    public int getAge() { return age; }

    public void setAge(int age) {this.age = age; }

}

When I run the app again, I get the error:

  

io.realm.exceptions.RealmMigrationNeededException: RealmMigration must   be provided

LogError:

06-01 08:35:54.755 21140-21140/realm.test.app.testrealm E/AndroidRuntime: FATAL EXCEPTION: main
   Process: realm.test.app.testrealm, PID: 21140
   java.lang.RuntimeException: Unable to start activity ComponentInfo{realm.test.app.testrealm/realm.test.app.testrealm.MainActivity}: io.realm.exceptions.RealmMigrationNeededException: RealmMigration must be provided
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
       at android.app.ActivityThread.access$800(ActivityThread.java:155)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:135)
       at android.app.ActivityThread.main(ActivityThread.java:5343)
       at java.lang.reflect.Method.invoke(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
    Caused by: io.realm.exceptions.RealmMigrationNeededException: RealmMigration must be provided
       at io.realm.BaseRealm.migrateRealm(BaseRealm.java:680)
       at io.realm.Realm.migrateRealm(Realm.java:1221)
       at io.realm.Realm.migrateRealm(Realm.java:1208)
       at io.realm.Realm.createInstance(Realm.java:235)
       at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:126)
       at io.realm.Realm.getDefaultInstance(Realm.java:174)
       at realm.test.app.testrealm.MainActivity.onCreate(MainActivity.java:27)
       at android.app.Activity.performCreate(Activity.java:6010)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) 
       at android.app.ActivityThread.access$800(ActivityThread.java:155) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) 
       at android.os.Handler.dispatchMessage(Handler.java:102) 
       at android.os.Looper.loop(Looper.java:135) 
       at android.app.ActivityThread.main(ActivityThread.java:5343) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at java.lang.reflect.Method.invoke(Method.java:372) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 

The error is that I need to define the method igration in MyApplication.java

public class MyApplication extends Application {

    private static final String TAG = "MyApplication";

    public void onCreate() {
        super.onCreate();
        Stetho.initialize(
                Stetho.newInitializerBuilder(this)
                        .enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
                        .enableWebKitInspector(RealmInspectorModulesProvider.builder(this).build())
                        .build());

        Log.d(TAG, "onCreate: Realm Object");

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

    }


}
    
asked by Webserveis 01.06.2016 в 09:23
source

1 answer

2

The problem is that when creating the database for the first time, the table / object Person only had two field id,name and then another field age was added.

  

io.realm.exceptions.RealmMigrationNeededException: RealmMigration must   be provided

A migration from the old to the new must be made.

One solution, the quickest is to empty the existing data with the method deleteRealmIfMigrationNeeded and then execute the method migration

public class MyApplication extends Application {

    private static final String TAG = "MyApplication";

    public void onCreate() {
        super.onCreate();
        Stetho.initialize(
                Stetho.newInitializerBuilder(this)
                        .enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
                        .enableWebKitInspector(RealmInspectorModulesProvider.builder(this).build())
                        .build());

        Log.d(TAG, "onCreate: Realm Object");

        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this)
                .schemaVersion(1)
                .deleteRealmIfMigrationNeeded()
                .migration(new MyMigration())
                .build();
        Realm.setDefaultConfiguration(realmConfiguration);

    }

    private class MyMigration implements RealmMigration {
        @Override
        public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
            Log.i(TAG, "migrate() called with: " + "realm = [" + realm + "], oldVersion = [" + oldVersion + "], newVersion = [" + newVersion + "]");
        }
    }
}
    
answered by 01.06.2016 / 09:23
source