0

I added a new simple RealmObject class (IoSteps) with just two integer fields and I keep getting a RealmMigrationNeededException. I have tried the suggestion here which is to use .deleteRealmIfMigrationNeeded() however, I continue to get the same error. And when I delete the class, the error goes away. Note that there are other RealmObject classes that have previously created. These are unchanged and I don't get any error having them in the code, only when I try to add a new RealmObject class. Below is my stacktrace:

08-10 12:45:48.972 18196-18196/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.fanny.traxivity, PID: 18196 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fanny.traxivity/com.fanny.traxivity.view.LoginActivity}: io.realm.exceptions.RealmMigrationNeededException: The 'IoSteps' class is missing from the schema for this Realm. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2924) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985) at android.app.ActivityThread.-wrap14(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6692) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358) Caused by: io.realm.exceptions.RealmMigrationNeededException: The 'IoSteps' class is missing from the schema for this Realm. at io.realm.IoStepsRealmProxy.validateTable(IoStepsRealmProxy.java:151) at io.realm.DefaultRealmModuleMediator.validateTable(DefaultRealmModuleMediator.java:68) at io.realm.Realm.initializeRealm(Realm.java:480) at io.realm.Realm.createAndValidateFromCache(Realm.java:423) at io.realm.Realm.createInstance(Realm.java:387) at io.realm.RealmCache.doCreateRealmOrGetFromCache(RealmCache.java:346) at io.realm.RealmCache.createRealmOrGetFromCache(RealmCache.java:284) at io.realm.Realm.getInstance(Realm.java:301) at com.fanny.traxivity.view.LoginActivity.onCreate(LoginActivity.java:53) at android.app.Activity.performCreate(Activity.java:6912) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)  at android.app.ActivityThread.-wrap14(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1635)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6692)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)

EKN
  • 1,886
  • 1
  • 16
  • 29
user3669557
  • 81
  • 1
  • 2
  • Hi @EpicPandaForce, as I mentioned in my question, I have tried calling deletRealmIfMigrationNeeded() on my configuration but this made no difference. I keep getting the exact same exception! Even after doing and Clean and Rebuild of the project. – user3669557 Aug 10 '17 at 13:12
  • That means you are not actually using the configuration that you set `deleteRealmIfMigrationNeeded()` on, but you're using the one set by calling `Realm.init()`. You need to call `Realm.setDefaultConfiguration()` to set a custom configuration as your default. – EpicPandaForce Aug 10 '17 at 13:28
  • @EpicPandaForce This is the code in the onCreate method of my MainActivity: Realm.init(this); RealmConfiguration realmConfig = new RealmConfiguration.Builder().deleteRealmIfMigrationNeeded().build(); Realm.setDefaultConfiguration(realmConfig); Realm.deleteRealm(realmConfig); And I still get the exception. – user3669557 Aug 10 '17 at 13:33
  • That's because you set that in MainActivity, but the crash is in LoginActivity.... – EpicPandaForce Aug 10 '17 at 13:37
  • Of course, because the Schema cannot be initialized with your new classes, because there is a mismatch with the file. You do not have your default RealmConfiguration set in `LoginActivity`, so you are not deleting on migration. Have you read the answer on the duplicate question? – EpicPandaForce Aug 10 '17 at 13:43
  • @EpicPandaForce nice observation. That was indeed the problem! I took out the Realm initialisation from LoginActivity and it works now. Thanks very much for your assistance. – user3669557 Aug 10 '17 at 13:45
  • Consider moving this code to `Application` class https://realm.io/docs/java/latest/#initializing-realm – EpicPandaForce Aug 10 '17 at 13:53
  • OK I'll do that. Thanks. – user3669557 Aug 10 '17 at 14:04
  • Best practise for me (if you dont want to deal with migrations) is simply wipe the data on the device on wich you installed your realm app, then install the app again with new schema. Problem solved – Vlad Skurtolov Aug 11 '17 at 10:11

0 Answers0