Android Realm Migration: Adding new Realm list column
Asked Answered
P

2

8

Im using Realm v0.80.1 and I am trying to write migration code for a new property I added. The property is a RealmList. Im not sure how to properly add the new column or set a a value.

What I have: customRealmTable.addColumn(, "list");

Once the column is properly added how would I go about setting an initial value for the list property? I would like to do something like:

customRealmTable.setRealmList(newColumnIndex, rowIndex, new RealmList<>());

Popularize answered 28/5, 2015 at 19:56 Comment(0)
I
7

You can see an example of adding a RealmList attribute in the examples here: https://github.com/realm/realm-java/blob/master/examples/migrationExample/src/main/java/io/realm/examples/realmmigrationexample/model/Migration.java#L78-L78

The relevant code is this section:

   if (version == 1) {
            Table personTable = realm.getTable(Person.class);
            Table petTable = realm.getTable(Pet.class);
            petTable.addColumn(ColumnType.STRING, "name");
            petTable.addColumn(ColumnType.STRING, "type");
            long petsIndex = personTable.addColumnLink(ColumnType.LINK_LIST, "pets", petTable);
            long fullNameIndex = getIndexForProperty(personTable, "fullName");

            for (int i = 0; i < personTable.size(); i++) {
                if (personTable.getString(fullNameIndex, i).equals("JP McDonald")) {
                    personTable.getRow(i).getLinkList(petsIndex).add(petTable.add("Jimbo", "dog"));
                }
            }
            version++;
        }
Illstarred answered 29/5, 2015 at 6:48 Comment(2)
This answer is outdated; take a look my answer below for how to do this with the latest Realm version.Octal
How to check the value of another field in RealmMigration and transform a new added field based on that. Suppose, I have a field "a" which is an integer, I want to add a new field "b" which is an integer. In Realm Migration, I have to check, If a == -1, then b == 0 else if a == 0 then b == 1 like that.Formfitting
O
16

As of Realm v1.0.0 (and maybe before), you can simply call RealmObjectSchema#addRealmListField(String, RealmObjectSchema) (link to javadoc) to achieve this. For example, if you're trying to add a permissions field of type RealmList<Permission> to your User class, you'd write:

if (!schema.get("User").hasField("permissions")) {
    schema.get("User").addRealmListField("permissions", schema.get("Permission"));
}

There is also an example in Realm's migration docs here. And here is the full javadoc for addRealmListField, for convenience:

/**
 * Adds a new field that references a {@link RealmList}.
 *
 * @param fieldName  name of the field to add.
 * @param objectSchema schema for the Realm type being referenced.
 * @return the updated schema.
 * @throws IllegalArgumentException if the field name is illegal or a field with that name already exists.
 */
Octal answered 5/10, 2016 at 21:11 Comment(1)
What exactly is permissions versus Permission?Hepzi
I
7

You can see an example of adding a RealmList attribute in the examples here: https://github.com/realm/realm-java/blob/master/examples/migrationExample/src/main/java/io/realm/examples/realmmigrationexample/model/Migration.java#L78-L78

The relevant code is this section:

   if (version == 1) {
            Table personTable = realm.getTable(Person.class);
            Table petTable = realm.getTable(Pet.class);
            petTable.addColumn(ColumnType.STRING, "name");
            petTable.addColumn(ColumnType.STRING, "type");
            long petsIndex = personTable.addColumnLink(ColumnType.LINK_LIST, "pets", petTable);
            long fullNameIndex = getIndexForProperty(personTable, "fullName");

            for (int i = 0; i < personTable.size(); i++) {
                if (personTable.getString(fullNameIndex, i).equals("JP McDonald")) {
                    personTable.getRow(i).getLinkList(petsIndex).add(petTable.add("Jimbo", "dog"));
                }
            }
            version++;
        }
Illstarred answered 29/5, 2015 at 6:48 Comment(2)
This answer is outdated; take a look my answer below for how to do this with the latest Realm version.Octal
How to check the value of another field in RealmMigration and transform a new added field based on that. Suppose, I have a field "a" which is an integer, I want to add a new field "b" which is an integer. In Realm Migration, I have to check, If a == -1, then b == 0 else if a == 0 then b == 1 like that.Formfitting

© 2022 - 2024 — McMap. All rights reserved.