Previous realm data is lost when Realm migration is done in android -


previously using 0.82.0 realm version , using 0.87.0 version.according project requirements attempting add 2 new tables existing realm schema.

for added below code..

public class unitdetails extends realmobject {      private int unituid;      private double noofrooms;      private int unitarea;      private int fid;      private int fnum;      private int maxavailablity;      // , setters , getter methods  }  public class quote extends realmobject {      private string qname;     private string qlocation;      // , setters , getter methods  } 

and migration code follows,creating realmconfiguration instance using appication context, , calling getrealminstance() method wherever realm instance needed.

public class realmutils  {      private static realmconfiguration config;      public static void createrealmconfig(context context) {           config = new realmconfiguration.builder(context)                 .name("default.realm1")                 .schemaversion(1)                 .migration(new custommigration())                 .build();     }      public static realm getrealminstance() {           return realm.getinstance(config);     } }  public class custommigration implements realmmigration {      @override     public void migrate(final dynamicrealm realm, long oldversion, long newversion) {         realmschema schema = realm.getschema();         if (oldversion == 0) {             realmobjectschema quoteschema = schema.create("quote")                     .addfield("qname", string.class)                     .addfield("qlocation", string.class);              realmobjectschema unitdetailsschema = schema.create("unitdetails")                     .addfield("unituid", int.class)                     .addfield("noofrooms", double.class)                     .addfield("unitarea", int.class)                     .addfield("fid", int.class)                     .addfield("fnum", int.class)                     .addfield("maxavailablity", int.class);             oldversion++;         }     } } 

when update app installing new apk, not getting realmmigrationneededexception previous data lost.

what going wrong here , how prevent data loss? need existing tables? should take care of, using new realm version ?

you should not update name well, version number. following configuration should work:

public class realmutils  {      private static realmconfiguration config;      public static void createrealmconfig(context context) {           config = new realmconfiguration.builder(context)                                                            .schemaversion(1)                 .migration(new custommigration())                 .build();     }      public static realm getrealminstance() {           return realm.getinstance(config);     } } 

Comments

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -