Kevin van Mierlo Kevin van Mierlo - 26 days ago 12
Android Question

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.


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() {
                            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: