realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

Fatal Exception: io.realm.exceptions.RealmMigrationNeededException

Open UmarBhutta opened this issue 8 years ago • 4 comments

Goal

Realm migration is always a crucial step for me, I have been trying to migrate my only model class, on some devices it is not an issue but on some devices it is producing crashes. I am not sure what to do. could you please help me out ?

Actual Results

Fatal Exception: io.realm.exceptions.RealmMigrationNeededException: Migration is required due to the following errors:

  • Property 'Receipt.receiptDate' has been added.
  • Property 'Receipt.localUrl' has been added. at io.realm.internal.OsSharedRealm.nativeGetSharedRealm(SourceFile) at io.realm.internal.OsSharedRealm.(SourceFile:171) at io.realm.internal.OsSharedRealm.getInstance(SourceFile:241) at io.realm.BaseRealm.(SourceFile:134) at io.realm.BaseRealm.(SourceFile:103) at io.realm.Realm.(SourceFile:161) at io.realm.Realm.createInstance(SourceFile:446) at io.realm.RealmCache.doCreateRealmOrGetFromCache(SourceFile:342) at io.realm.RealmCache.createRealmOrGetFromCache(SourceFile:282) at io.realm.Realm.getInstance(SourceFile:375) at com.mallconnect.tdm.redeem.RedeemFragment.onCreate(SourceFile:98) at android.support.v4.app.Fragment.performCreate(SourceFile:2331) at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1386) at android.support.v4.app.FragmentTransition.addToFirstInLastOut(SourceFile:1188) at android.support.v4.app.FragmentTransition.calculateFragments(SourceFile:1071) at android.support.v4.app.FragmentTransition.startTransitions(SourceFile:115) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(SourceFile:2380) at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(SourceFile:2338) at android.support.v4.app.FragmentManagerImpl.execPendingActions(SourceFile:2245) at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(SourceFile:3248) at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(SourceFile:3200) at android.support.v4.app.Fragment.performActivityCreated(SourceFile:2361) at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1451) at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(SourceFile:1759) at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1827) at android.support.v4.app.BackStackRecord.executeOps(SourceFile:797) at android.support.v4.app.FragmentManagerImpl.executeOps(SourceFile:2596) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(SourceFile:2383) at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(SourceFile:2338) at android.support.v4.app.FragmentManagerImpl.execPendingActions(SourceFile:2245) at android.support.v4.app.FragmentManagerImpl$1.run(SourceFile:703) at android.os.Handler.handleCallback(Handler.java:789) at android.os.Handler.dispatchMessage(Handler.java:98) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6938) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

Steps & Code to Reproduce

Realm configuration realmConfiguration = new RealmConfiguration.Builder() .name("tdm.realm") .schemaVersion(4) .migration(new RealmMigrations()) .build(); Migration class public class RealmMigrations implements RealmMigration {

@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
    final RealmSchema schema = realm.getSchema();
    if (oldVersion == 1){
        final RealmObjectSchema userObjectSchema = schema.get("Receipt");
        if (userObjectSchema != null) {
            userObjectSchema.addField("miles",int.class);
        }
        oldVersion++;
    }else  if (oldVersion == 2){
        final RealmObjectSchema userObjectSchema = schema.get("Receipt");
        if (userObjectSchema != null) {
            userObjectSchema.addField("rejectReason",String.class);
        }
        oldVersion++;
    }else  if (oldVersion == 3){
        final RealmObjectSchema userObjectSchema = schema.get("Receipt");
        if (userObjectSchema != null){
            userObjectSchema.addField("receiptDate",String.class);
            userObjectSchema.addField("localUrl",String.class);
        }
        oldVersion++;
    }
}

@Override
public int hashCode() {
    return 101;
}

@Override
public boolean equals(Object obj) {
    return obj instanceof RealmMigrations;
}

}

i am not able to produce it on my testing device but i am getting crashes on the fabric and crash count is high which is pretty alarming for statbility

Version of Realm and tooling

Realm version(s): 5.7.0

Realm Sync feature enabled: No

Android Studio version: 3.2

Android Build Tools version: 27.0.3

Gradle version: 3.1.4

Which Android version and device(s): Devices: Samsung SM-G965F SM-N950F Galaxy S7 SM-G955F Galaxy S7 Edge SM-A605F9 Galaxy Note SM-G960F Galaxy Note5

Huwaeii: WAS-LX16 VTR-L29

OS: 8.0.0 8.1.0 7.0 6.0 6.0.1 5.1.1

UmarBhutta avatar Oct 20 '18 14:10 UmarBhutta

Your migration code is not correct.

You should not be using 'else if' at all.

If a user upgrades from v1 to v3, it will only run the migration code for v3, never v1/v2.

Remove all "else if" and replace them with simple "if"s, and it should work a lot better.

@Override
public void migrate(DynamicRealm realm, long oldVersion, long newVersion) {
    final RealmSchema schema = realm.getSchema();
    if (oldVersion == 1){
        final RealmObjectSchema userObjectSchema = schema.get("Receipt");
        if (userObjectSchema != null) {
            userObjectSchema.addField("miles",int.class);
        }
        oldVersion++;
    }
    if (oldVersion == 2){
        final RealmObjectSchema userObjectSchema = schema.get("Receipt");
        if (userObjectSchema != null) {
            userObjectSchema.addField("rejectReason",String.class);
        }
        oldVersion++;
    }
    if (oldVersion == 3){
        final RealmObjectSchema userObjectSchema = schema.get("Receipt");
        if (userObjectSchema != null){
            userObjectSchema.addField("receiptDate",String.class);
            userObjectSchema.addField("localUrl",String.class);
        }
        oldVersion++;
    }
}

Moonbloom avatar Oct 20 '18 20:10 Moonbloom

@Moonbloom Thanks for the quick response. i am implementing that solution and let you know how that works.

UmarBhutta avatar Oct 22 '18 07:10 UmarBhutta

I assume the above worked, so closing.

cmelchior avatar Mar 19 '19 14:03 cmelchior

Hey - looks like you forgot to add a T:* label - could you please add one?

realm-probot avatar Mar 19 '19 14:03 realm-probot