How to update initial data in realm after update of apk?How do I pass data between Activities in Android application?How to avoid reverse engineering of an APK file?Update Realm database versionLoosing previous realm data when I do migration in androidMigration of one of two realm instanceHow to handle Realm React Native migrations and schemaVersion on iOS?Error copying object to RealmHow to migrate multiple realm files in one AppError: Realm at path '/var/…/default.realm' already opened on current thread with different schemaHow to do a Realm migration in Android

What is this word supposed to be?

What is purpose of DB Browser(dbbrowser.aspx) under admin tool?

Negative Resistance

"My boss was furious with me and I have been fired" vs. "My boss was furious with me and I was fired"

Multiple fireplaces in an apartment building?

Co-worker works way more than he should

Which big number is bigger?

How to not starve gigantic beasts

Why did Rep. Omar conclude her criticism of US troops with the phrase "NotTodaySatan"?

Can a stored procedure reference the database in which it is stored?

Is this a typo in Section 1.8.1 Mathematics for Computer Science?

Multiple options vs single option UI

Retract an already submitted recommendation letter (written for an undergrad student)

A Paper Record is What I Hamper

What is the best way to deal with NPC-NPC combat?

Extracting Dirichlet series coefficients

What makes accurate emulation of old systems a difficult task?

Do I need to watch Ant-Man and the Wasp and Captain Marvel before watching Avengers: Endgame?

Why do distances seem to matter in the Foundation world?

Why must Chinese maps be obfuscated?

A ​Note ​on ​N!

Why do games have consumables?

How to be good at coming up with counter example in Topology

Can someone publish a story that happened to you?



How to update initial data in realm after update of apk?


How do I pass data between Activities in Android application?How to avoid reverse engineering of an APK file?Update Realm database versionLoosing previous realm data when I do migration in androidMigration of one of two realm instanceHow to handle Realm React Native migrations and schemaVersion on iOS?Error copying object to RealmHow to migrate multiple realm files in one AppError: Realm at path '/var/…/default.realm' already opened on current thread with different schemaHow to do a Realm migration in Android






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















In the app there is a realm database that has both initial data and data added by the user. The data added by the user should stay on the device even after the updates. The initial data is added through the class



public class RealmInitialData implements Realm.Transaction 

@Override
public void execute(Realm realm)
//initial data
Items item = new Items();
item.setName("Fragrance");
item.setTimestamp(System.currentTimeMillis());
realm.insertOrUpdate(item);

@Override
public int hashCode()
return RealmInitialData.class.hashCode();


@Override
public boolean equals(Object obj)
return obj != null && obj instanceof RealmInitialData;




The configuration (situated in a class which extends Application class) looks like this:



RealmConfiguration realmConfig = new RealmConfiguration.Builder()
.name("tasky.realm")
.schemaVersion(2)
.migration(new Migration())
.initialData(new RealmInitialData())
.build();


If I change/add something in the RealmInitialData for update, the data on the user application doesn't change. How can I both change the initial data (add or rewrite sth) and make no changes to the user's data?










share|improve this question




























    0















    In the app there is a realm database that has both initial data and data added by the user. The data added by the user should stay on the device even after the updates. The initial data is added through the class



    public class RealmInitialData implements Realm.Transaction 

    @Override
    public void execute(Realm realm)
    //initial data
    Items item = new Items();
    item.setName("Fragrance");
    item.setTimestamp(System.currentTimeMillis());
    realm.insertOrUpdate(item);

    @Override
    public int hashCode()
    return RealmInitialData.class.hashCode();


    @Override
    public boolean equals(Object obj)
    return obj != null && obj instanceof RealmInitialData;




    The configuration (situated in a class which extends Application class) looks like this:



    RealmConfiguration realmConfig = new RealmConfiguration.Builder()
    .name("tasky.realm")
    .schemaVersion(2)
    .migration(new Migration())
    .initialData(new RealmInitialData())
    .build();


    If I change/add something in the RealmInitialData for update, the data on the user application doesn't change. How can I both change the initial data (add or rewrite sth) and make no changes to the user's data?










    share|improve this question
























      0












      0








      0








      In the app there is a realm database that has both initial data and data added by the user. The data added by the user should stay on the device even after the updates. The initial data is added through the class



      public class RealmInitialData implements Realm.Transaction 

      @Override
      public void execute(Realm realm)
      //initial data
      Items item = new Items();
      item.setName("Fragrance");
      item.setTimestamp(System.currentTimeMillis());
      realm.insertOrUpdate(item);

      @Override
      public int hashCode()
      return RealmInitialData.class.hashCode();


      @Override
      public boolean equals(Object obj)
      return obj != null && obj instanceof RealmInitialData;




      The configuration (situated in a class which extends Application class) looks like this:



      RealmConfiguration realmConfig = new RealmConfiguration.Builder()
      .name("tasky.realm")
      .schemaVersion(2)
      .migration(new Migration())
      .initialData(new RealmInitialData())
      .build();


      If I change/add something in the RealmInitialData for update, the data on the user application doesn't change. How can I both change the initial data (add or rewrite sth) and make no changes to the user's data?










      share|improve this question














      In the app there is a realm database that has both initial data and data added by the user. The data added by the user should stay on the device even after the updates. The initial data is added through the class



      public class RealmInitialData implements Realm.Transaction 

      @Override
      public void execute(Realm realm)
      //initial data
      Items item = new Items();
      item.setName("Fragrance");
      item.setTimestamp(System.currentTimeMillis());
      realm.insertOrUpdate(item);

      @Override
      public int hashCode()
      return RealmInitialData.class.hashCode();


      @Override
      public boolean equals(Object obj)
      return obj != null && obj instanceof RealmInitialData;




      The configuration (situated in a class which extends Application class) looks like this:



      RealmConfiguration realmConfig = new RealmConfiguration.Builder()
      .name("tasky.realm")
      .schemaVersion(2)
      .migration(new Migration())
      .initialData(new RealmInitialData())
      .build();


      If I change/add something in the RealmInitialData for update, the data on the user application doesn't change. How can I both change the initial data (add or rewrite sth) and make no changes to the user's data?







      android realm






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 22 at 16:44









      JuliaJulia

      2417




      2417






















          2 Answers
          2






          active

          oldest

          votes


















          0














          You have to implement RealmMigration and pass it to RealmConfiguration.Builder().migration().
          Something like this:



          public class RealmMigrationAgent implements RealmMigration

          @Override
          public void migrate(DynamicRealm realm, long oldVersion, long newVersion)
          RealmSchema schema = realm.getSchema();

          if (oldVersion == 0)
          schema.get("InventDisableObject").addField("Reason", int.class);
          oldVersion++;


          if (oldVersion == 1)
          if (!schema.get("ActionObject").hasField("Annual"))
          schema.get("ActionObject").addField("Annual", boolean.class);
          oldVersion++;

          if (oldVersion == 2)
          if (!schema.get("ActionObject").hasField("Hash"))
          schema.get("ActionObject").addField("Hash", String.class);
          if (!schema.get("ActionObject").hasField("Manual"))
          schema.get("ActionObject").addField("Manual", boolean.class);
          oldVersion++;

          if (oldVersion == 3)
          if (!schema.get("OutletObject").hasField("Code"))
          schema.get("OutletObject").addField("Code", String.class);
          oldVersion++;





          Incrementally adding scheme updates for every new scheme version.
          I can see you have some class Migration for migration reason - can you provide the code of it?






          share|improve this answer























          • Basically, I don't have any field added/deleted from my models class. I need just to change database initial data after an update. So the migration won't solve the problem. The problem is in this string .initialData(new RealmInitialData()) - it goes there only when the application is installed. I need it to go there after update too.

            – Julia
            Mar 26 at 15:58











          • help to initialData() says it only works in 2 cases: new database created and if deleteRealmIfMigrationisNeeded (or similar - can't remember exactly)set to true (which also destroys any data in database). So if you need data to be added and keep previous data - you can I assume - up scheme version and use migration agent to strictly add data, or use some value in database for last version, check it against new version and if update happened - add it, or something like that.

            – Maxim Berezovsky
            Mar 26 at 16:04












          • Ok. What should I write then in migration to "strictly add data"? Will it go again into the RealmInitialData class or I need to create another class to add these new components. And how should I include this in migration?

            – Julia
            Mar 26 at 16:39











          • probably the same code that you use to add objects in RealmInitalData. Or create the object of it, if data is added in onCreate() or constructor. if (oldVersion == 0) // create your initial data object here that suitable for this version ... don't forget keep all history of versions in so user can update from any version to last. the code will be executed incrementally for ever new version.

            – Maxim Berezovsky
            Mar 26 at 16:55












          • never used Migration class to add any data though - might have some issues.. better if you tried 2nd variant first.

            – Maxim Berezovsky
            Mar 26 at 16:59


















          0














          I have the same problem and i find the way.For example,if your new version is 7,try to use SharedRealm.getInstance(config).getSchemaVersion() to get the old version 6 before Realm.setDefaultConfiguration(config),then you can update old realm data after setDefaultConfiguration which old version < 7.May this can help you:



          RealmConfiguration config = new RealmConfiguration.Builder()
          .schemaVersion(7)
          .migration(new RealmMigration()
          @Override
          public void migrate(DynamicRealm realm, long oldVersion, long newVersion)
          RealmSchema schema = realm.getSchema();
          if(oldVersion == 6)
          schema.get("ImageRealm")
          .addField("isSelected", Boolean.class, FieldAttribute.REQUIRED)
          .addField("isLow", Boolean.class, FieldAttribute.REQUIRED);
          oldVersion++;


          ).build();

          SharedRealm sharedRealm = SharedRealm.getInstance(config);
          long oldVersion = sharedRealm.getSchemaVersion();

          Realm.setDefaultConfiguration(config);
          long newVersion = Realm.getDefaultInstance().getVersion();

          // 如果有历史记录,更新状态
          if (oldVersion < 7)
          Realm.getDefaultInstance().executeTransaction(new Realm.Transaction()
          @Override
          public void execute(Realm realm)
          RealmResults<DocRealmBean> allData = realm.where(DocRealmBean.class).findAll();
          for (DocRealmBean bean : allData)
          bean.state = "0";


          );






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304260%2fhow-to-update-initial-data-in-realm-after-update-of-apk%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You have to implement RealmMigration and pass it to RealmConfiguration.Builder().migration().
            Something like this:



            public class RealmMigrationAgent implements RealmMigration

            @Override
            public void migrate(DynamicRealm realm, long oldVersion, long newVersion)
            RealmSchema schema = realm.getSchema();

            if (oldVersion == 0)
            schema.get("InventDisableObject").addField("Reason", int.class);
            oldVersion++;


            if (oldVersion == 1)
            if (!schema.get("ActionObject").hasField("Annual"))
            schema.get("ActionObject").addField("Annual", boolean.class);
            oldVersion++;

            if (oldVersion == 2)
            if (!schema.get("ActionObject").hasField("Hash"))
            schema.get("ActionObject").addField("Hash", String.class);
            if (!schema.get("ActionObject").hasField("Manual"))
            schema.get("ActionObject").addField("Manual", boolean.class);
            oldVersion++;

            if (oldVersion == 3)
            if (!schema.get("OutletObject").hasField("Code"))
            schema.get("OutletObject").addField("Code", String.class);
            oldVersion++;





            Incrementally adding scheme updates for every new scheme version.
            I can see you have some class Migration for migration reason - can you provide the code of it?






            share|improve this answer























            • Basically, I don't have any field added/deleted from my models class. I need just to change database initial data after an update. So the migration won't solve the problem. The problem is in this string .initialData(new RealmInitialData()) - it goes there only when the application is installed. I need it to go there after update too.

              – Julia
              Mar 26 at 15:58











            • help to initialData() says it only works in 2 cases: new database created and if deleteRealmIfMigrationisNeeded (or similar - can't remember exactly)set to true (which also destroys any data in database). So if you need data to be added and keep previous data - you can I assume - up scheme version and use migration agent to strictly add data, or use some value in database for last version, check it against new version and if update happened - add it, or something like that.

              – Maxim Berezovsky
              Mar 26 at 16:04












            • Ok. What should I write then in migration to "strictly add data"? Will it go again into the RealmInitialData class or I need to create another class to add these new components. And how should I include this in migration?

              – Julia
              Mar 26 at 16:39











            • probably the same code that you use to add objects in RealmInitalData. Or create the object of it, if data is added in onCreate() or constructor. if (oldVersion == 0) // create your initial data object here that suitable for this version ... don't forget keep all history of versions in so user can update from any version to last. the code will be executed incrementally for ever new version.

              – Maxim Berezovsky
              Mar 26 at 16:55












            • never used Migration class to add any data though - might have some issues.. better if you tried 2nd variant first.

              – Maxim Berezovsky
              Mar 26 at 16:59















            0














            You have to implement RealmMigration and pass it to RealmConfiguration.Builder().migration().
            Something like this:



            public class RealmMigrationAgent implements RealmMigration

            @Override
            public void migrate(DynamicRealm realm, long oldVersion, long newVersion)
            RealmSchema schema = realm.getSchema();

            if (oldVersion == 0)
            schema.get("InventDisableObject").addField("Reason", int.class);
            oldVersion++;


            if (oldVersion == 1)
            if (!schema.get("ActionObject").hasField("Annual"))
            schema.get("ActionObject").addField("Annual", boolean.class);
            oldVersion++;

            if (oldVersion == 2)
            if (!schema.get("ActionObject").hasField("Hash"))
            schema.get("ActionObject").addField("Hash", String.class);
            if (!schema.get("ActionObject").hasField("Manual"))
            schema.get("ActionObject").addField("Manual", boolean.class);
            oldVersion++;

            if (oldVersion == 3)
            if (!schema.get("OutletObject").hasField("Code"))
            schema.get("OutletObject").addField("Code", String.class);
            oldVersion++;





            Incrementally adding scheme updates for every new scheme version.
            I can see you have some class Migration for migration reason - can you provide the code of it?






            share|improve this answer























            • Basically, I don't have any field added/deleted from my models class. I need just to change database initial data after an update. So the migration won't solve the problem. The problem is in this string .initialData(new RealmInitialData()) - it goes there only when the application is installed. I need it to go there after update too.

              – Julia
              Mar 26 at 15:58











            • help to initialData() says it only works in 2 cases: new database created and if deleteRealmIfMigrationisNeeded (or similar - can't remember exactly)set to true (which also destroys any data in database). So if you need data to be added and keep previous data - you can I assume - up scheme version and use migration agent to strictly add data, or use some value in database for last version, check it against new version and if update happened - add it, or something like that.

              – Maxim Berezovsky
              Mar 26 at 16:04












            • Ok. What should I write then in migration to "strictly add data"? Will it go again into the RealmInitialData class or I need to create another class to add these new components. And how should I include this in migration?

              – Julia
              Mar 26 at 16:39











            • probably the same code that you use to add objects in RealmInitalData. Or create the object of it, if data is added in onCreate() or constructor. if (oldVersion == 0) // create your initial data object here that suitable for this version ... don't forget keep all history of versions in so user can update from any version to last. the code will be executed incrementally for ever new version.

              – Maxim Berezovsky
              Mar 26 at 16:55












            • never used Migration class to add any data though - might have some issues.. better if you tried 2nd variant first.

              – Maxim Berezovsky
              Mar 26 at 16:59













            0












            0








            0







            You have to implement RealmMigration and pass it to RealmConfiguration.Builder().migration().
            Something like this:



            public class RealmMigrationAgent implements RealmMigration

            @Override
            public void migrate(DynamicRealm realm, long oldVersion, long newVersion)
            RealmSchema schema = realm.getSchema();

            if (oldVersion == 0)
            schema.get("InventDisableObject").addField("Reason", int.class);
            oldVersion++;


            if (oldVersion == 1)
            if (!schema.get("ActionObject").hasField("Annual"))
            schema.get("ActionObject").addField("Annual", boolean.class);
            oldVersion++;

            if (oldVersion == 2)
            if (!schema.get("ActionObject").hasField("Hash"))
            schema.get("ActionObject").addField("Hash", String.class);
            if (!schema.get("ActionObject").hasField("Manual"))
            schema.get("ActionObject").addField("Manual", boolean.class);
            oldVersion++;

            if (oldVersion == 3)
            if (!schema.get("OutletObject").hasField("Code"))
            schema.get("OutletObject").addField("Code", String.class);
            oldVersion++;





            Incrementally adding scheme updates for every new scheme version.
            I can see you have some class Migration for migration reason - can you provide the code of it?






            share|improve this answer













            You have to implement RealmMigration and pass it to RealmConfiguration.Builder().migration().
            Something like this:



            public class RealmMigrationAgent implements RealmMigration

            @Override
            public void migrate(DynamicRealm realm, long oldVersion, long newVersion)
            RealmSchema schema = realm.getSchema();

            if (oldVersion == 0)
            schema.get("InventDisableObject").addField("Reason", int.class);
            oldVersion++;


            if (oldVersion == 1)
            if (!schema.get("ActionObject").hasField("Annual"))
            schema.get("ActionObject").addField("Annual", boolean.class);
            oldVersion++;

            if (oldVersion == 2)
            if (!schema.get("ActionObject").hasField("Hash"))
            schema.get("ActionObject").addField("Hash", String.class);
            if (!schema.get("ActionObject").hasField("Manual"))
            schema.get("ActionObject").addField("Manual", boolean.class);
            oldVersion++;

            if (oldVersion == 3)
            if (!schema.get("OutletObject").hasField("Code"))
            schema.get("OutletObject").addField("Code", String.class);
            oldVersion++;





            Incrementally adding scheme updates for every new scheme version.
            I can see you have some class Migration for migration reason - can you provide the code of it?







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 25 at 8:36









            Maxim BerezovskyMaxim Berezovsky

            45748




            45748












            • Basically, I don't have any field added/deleted from my models class. I need just to change database initial data after an update. So the migration won't solve the problem. The problem is in this string .initialData(new RealmInitialData()) - it goes there only when the application is installed. I need it to go there after update too.

              – Julia
              Mar 26 at 15:58











            • help to initialData() says it only works in 2 cases: new database created and if deleteRealmIfMigrationisNeeded (or similar - can't remember exactly)set to true (which also destroys any data in database). So if you need data to be added and keep previous data - you can I assume - up scheme version and use migration agent to strictly add data, or use some value in database for last version, check it against new version and if update happened - add it, or something like that.

              – Maxim Berezovsky
              Mar 26 at 16:04












            • Ok. What should I write then in migration to "strictly add data"? Will it go again into the RealmInitialData class or I need to create another class to add these new components. And how should I include this in migration?

              – Julia
              Mar 26 at 16:39











            • probably the same code that you use to add objects in RealmInitalData. Or create the object of it, if data is added in onCreate() or constructor. if (oldVersion == 0) // create your initial data object here that suitable for this version ... don't forget keep all history of versions in so user can update from any version to last. the code will be executed incrementally for ever new version.

              – Maxim Berezovsky
              Mar 26 at 16:55












            • never used Migration class to add any data though - might have some issues.. better if you tried 2nd variant first.

              – Maxim Berezovsky
              Mar 26 at 16:59

















            • Basically, I don't have any field added/deleted from my models class. I need just to change database initial data after an update. So the migration won't solve the problem. The problem is in this string .initialData(new RealmInitialData()) - it goes there only when the application is installed. I need it to go there after update too.

              – Julia
              Mar 26 at 15:58











            • help to initialData() says it only works in 2 cases: new database created and if deleteRealmIfMigrationisNeeded (or similar - can't remember exactly)set to true (which also destroys any data in database). So if you need data to be added and keep previous data - you can I assume - up scheme version and use migration agent to strictly add data, or use some value in database for last version, check it against new version and if update happened - add it, or something like that.

              – Maxim Berezovsky
              Mar 26 at 16:04












            • Ok. What should I write then in migration to "strictly add data"? Will it go again into the RealmInitialData class or I need to create another class to add these new components. And how should I include this in migration?

              – Julia
              Mar 26 at 16:39











            • probably the same code that you use to add objects in RealmInitalData. Or create the object of it, if data is added in onCreate() or constructor. if (oldVersion == 0) // create your initial data object here that suitable for this version ... don't forget keep all history of versions in so user can update from any version to last. the code will be executed incrementally for ever new version.

              – Maxim Berezovsky
              Mar 26 at 16:55












            • never used Migration class to add any data though - might have some issues.. better if you tried 2nd variant first.

              – Maxim Berezovsky
              Mar 26 at 16:59
















            Basically, I don't have any field added/deleted from my models class. I need just to change database initial data after an update. So the migration won't solve the problem. The problem is in this string .initialData(new RealmInitialData()) - it goes there only when the application is installed. I need it to go there after update too.

            – Julia
            Mar 26 at 15:58





            Basically, I don't have any field added/deleted from my models class. I need just to change database initial data after an update. So the migration won't solve the problem. The problem is in this string .initialData(new RealmInitialData()) - it goes there only when the application is installed. I need it to go there after update too.

            – Julia
            Mar 26 at 15:58













            help to initialData() says it only works in 2 cases: new database created and if deleteRealmIfMigrationisNeeded (or similar - can't remember exactly)set to true (which also destroys any data in database). So if you need data to be added and keep previous data - you can I assume - up scheme version and use migration agent to strictly add data, or use some value in database for last version, check it against new version and if update happened - add it, or something like that.

            – Maxim Berezovsky
            Mar 26 at 16:04






            help to initialData() says it only works in 2 cases: new database created and if deleteRealmIfMigrationisNeeded (or similar - can't remember exactly)set to true (which also destroys any data in database). So if you need data to be added and keep previous data - you can I assume - up scheme version and use migration agent to strictly add data, or use some value in database for last version, check it against new version and if update happened - add it, or something like that.

            – Maxim Berezovsky
            Mar 26 at 16:04














            Ok. What should I write then in migration to "strictly add data"? Will it go again into the RealmInitialData class or I need to create another class to add these new components. And how should I include this in migration?

            – Julia
            Mar 26 at 16:39





            Ok. What should I write then in migration to "strictly add data"? Will it go again into the RealmInitialData class or I need to create another class to add these new components. And how should I include this in migration?

            – Julia
            Mar 26 at 16:39













            probably the same code that you use to add objects in RealmInitalData. Or create the object of it, if data is added in onCreate() or constructor. if (oldVersion == 0) // create your initial data object here that suitable for this version ... don't forget keep all history of versions in so user can update from any version to last. the code will be executed incrementally for ever new version.

            – Maxim Berezovsky
            Mar 26 at 16:55






            probably the same code that you use to add objects in RealmInitalData. Or create the object of it, if data is added in onCreate() or constructor. if (oldVersion == 0) // create your initial data object here that suitable for this version ... don't forget keep all history of versions in so user can update from any version to last. the code will be executed incrementally for ever new version.

            – Maxim Berezovsky
            Mar 26 at 16:55














            never used Migration class to add any data though - might have some issues.. better if you tried 2nd variant first.

            – Maxim Berezovsky
            Mar 26 at 16:59





            never used Migration class to add any data though - might have some issues.. better if you tried 2nd variant first.

            – Maxim Berezovsky
            Mar 26 at 16:59













            0














            I have the same problem and i find the way.For example,if your new version is 7,try to use SharedRealm.getInstance(config).getSchemaVersion() to get the old version 6 before Realm.setDefaultConfiguration(config),then you can update old realm data after setDefaultConfiguration which old version < 7.May this can help you:



            RealmConfiguration config = new RealmConfiguration.Builder()
            .schemaVersion(7)
            .migration(new RealmMigration()
            @Override
            public void migrate(DynamicRealm realm, long oldVersion, long newVersion)
            RealmSchema schema = realm.getSchema();
            if(oldVersion == 6)
            schema.get("ImageRealm")
            .addField("isSelected", Boolean.class, FieldAttribute.REQUIRED)
            .addField("isLow", Boolean.class, FieldAttribute.REQUIRED);
            oldVersion++;


            ).build();

            SharedRealm sharedRealm = SharedRealm.getInstance(config);
            long oldVersion = sharedRealm.getSchemaVersion();

            Realm.setDefaultConfiguration(config);
            long newVersion = Realm.getDefaultInstance().getVersion();

            // 如果有历史记录,更新状态
            if (oldVersion < 7)
            Realm.getDefaultInstance().executeTransaction(new Realm.Transaction()
            @Override
            public void execute(Realm realm)
            RealmResults<DocRealmBean> allData = realm.where(DocRealmBean.class).findAll();
            for (DocRealmBean bean : allData)
            bean.state = "0";


            );






            share|improve this answer





























              0














              I have the same problem and i find the way.For example,if your new version is 7,try to use SharedRealm.getInstance(config).getSchemaVersion() to get the old version 6 before Realm.setDefaultConfiguration(config),then you can update old realm data after setDefaultConfiguration which old version < 7.May this can help you:



              RealmConfiguration config = new RealmConfiguration.Builder()
              .schemaVersion(7)
              .migration(new RealmMigration()
              @Override
              public void migrate(DynamicRealm realm, long oldVersion, long newVersion)
              RealmSchema schema = realm.getSchema();
              if(oldVersion == 6)
              schema.get("ImageRealm")
              .addField("isSelected", Boolean.class, FieldAttribute.REQUIRED)
              .addField("isLow", Boolean.class, FieldAttribute.REQUIRED);
              oldVersion++;


              ).build();

              SharedRealm sharedRealm = SharedRealm.getInstance(config);
              long oldVersion = sharedRealm.getSchemaVersion();

              Realm.setDefaultConfiguration(config);
              long newVersion = Realm.getDefaultInstance().getVersion();

              // 如果有历史记录,更新状态
              if (oldVersion < 7)
              Realm.getDefaultInstance().executeTransaction(new Realm.Transaction()
              @Override
              public void execute(Realm realm)
              RealmResults<DocRealmBean> allData = realm.where(DocRealmBean.class).findAll();
              for (DocRealmBean bean : allData)
              bean.state = "0";


              );






              share|improve this answer



























                0












                0








                0







                I have the same problem and i find the way.For example,if your new version is 7,try to use SharedRealm.getInstance(config).getSchemaVersion() to get the old version 6 before Realm.setDefaultConfiguration(config),then you can update old realm data after setDefaultConfiguration which old version < 7.May this can help you:



                RealmConfiguration config = new RealmConfiguration.Builder()
                .schemaVersion(7)
                .migration(new RealmMigration()
                @Override
                public void migrate(DynamicRealm realm, long oldVersion, long newVersion)
                RealmSchema schema = realm.getSchema();
                if(oldVersion == 6)
                schema.get("ImageRealm")
                .addField("isSelected", Boolean.class, FieldAttribute.REQUIRED)
                .addField("isLow", Boolean.class, FieldAttribute.REQUIRED);
                oldVersion++;


                ).build();

                SharedRealm sharedRealm = SharedRealm.getInstance(config);
                long oldVersion = sharedRealm.getSchemaVersion();

                Realm.setDefaultConfiguration(config);
                long newVersion = Realm.getDefaultInstance().getVersion();

                // 如果有历史记录,更新状态
                if (oldVersion < 7)
                Realm.getDefaultInstance().executeTransaction(new Realm.Transaction()
                @Override
                public void execute(Realm realm)
                RealmResults<DocRealmBean> allData = realm.where(DocRealmBean.class).findAll();
                for (DocRealmBean bean : allData)
                bean.state = "0";


                );






                share|improve this answer















                I have the same problem and i find the way.For example,if your new version is 7,try to use SharedRealm.getInstance(config).getSchemaVersion() to get the old version 6 before Realm.setDefaultConfiguration(config),then you can update old realm data after setDefaultConfiguration which old version < 7.May this can help you:



                RealmConfiguration config = new RealmConfiguration.Builder()
                .schemaVersion(7)
                .migration(new RealmMigration()
                @Override
                public void migrate(DynamicRealm realm, long oldVersion, long newVersion)
                RealmSchema schema = realm.getSchema();
                if(oldVersion == 6)
                schema.get("ImageRealm")
                .addField("isSelected", Boolean.class, FieldAttribute.REQUIRED)
                .addField("isLow", Boolean.class, FieldAttribute.REQUIRED);
                oldVersion++;


                ).build();

                SharedRealm sharedRealm = SharedRealm.getInstance(config);
                long oldVersion = sharedRealm.getSchemaVersion();

                Realm.setDefaultConfiguration(config);
                long newVersion = Realm.getDefaultInstance().getVersion();

                // 如果有历史记录,更新状态
                if (oldVersion < 7)
                Realm.getDefaultInstance().executeTransaction(new Realm.Transaction()
                @Override
                public void execute(Realm realm)
                RealmResults<DocRealmBean> allData = realm.where(DocRealmBean.class).findAll();
                for (DocRealmBean bean : allData)
                bean.state = "0";


                );







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Apr 9 at 14:27

























                answered Apr 9 at 13:56









                sunday_暖阳sunday_暖阳

                12




                12



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55304260%2fhow-to-update-initial-data-in-realm-after-update-of-apk%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript