Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Realm Migration version number based on what?

I'm doing my first Realm migration and started thinking about the version number. On what is this version number based?

Because if it is based on what is on your phone, how do I handle it if a new person installs the app and gets a migration? Because it will also update the fields which where already set because of a fresh install.

like image 388
Kevin van Mierlo Avatar asked Jan 08 '23 11:01

Kevin van Mierlo


1 Answers

Christian from Realm here. The migration API is still in a very experimental state and kinda ugly, so right now the version number always start with 0, and the only way for changing that is through a migration.

This means that if you want a fresh install with a different version other than 0, you will have to do something like:

   // Pseudo code
    public class RealmHelper() {

        private static SharedPreferences prefs;

        public static Realm getInstance() {
            if (!prefs.getBoolean("versionSet", false)) {
                String path = new File(context.getFilesDir(), Realm.DEFAULT_REALM_NAME).getAbsolutePath();
                Realm.migrateRealmAtPath(path, new RealmMigration() {
                            @Override
                            public long execute(Realm realm, long version) {
                                return 42; // Set version numbers
                            }
                        })
                prefs.edit().putBoolean("versionSet", true).apply();
            }

            return Realm.getInstance();
        }
    }

This is going to be a lot better soon though: https://github.com/realm/realm-java/pull/929

like image 149
Christian Melchior Avatar answered Jan 14 '23 09:01

Christian Melchior