Change Realm Field Data Type - Java

I would like to change the data type of the Realm field from Stringto intand FYI, this field is also Primary Key. I could not find a method in RealmMigrationto solve this problem.

PS: My application is already ready, and all the values ​​that are currently in this field are integers.

EDIT 1

My model class

public class Team extends RealmObject {
    @SerializedName("id")
    @PrimaryKey
    private int id;
    @SerializedName("name")
    private String name;
    @SerializedName("description")
    private String description;
}

my migration after responding to the christian answer

if (oldVersion == 6) {
            RealmObjectSchema teamSchema = schema.get("Team");
            teamSchema.addField("temp_id", int.class)
                    .transform(new RealmObjectSchema.Function() {
                        @Override
                        public void apply(DynamicRealmObject obj) {
                            obj.setInt("temp_id", Integer.valueOf(obj.getString("id")));
                        }
                    })
                    .removeField("id")
                    .renameField("temp_id", "id")
                    .addPrimaryKey("id");
        }
+4
source share
1 answer

, , : https://github.com/realm/realm-java/blob/master/examples/migrationExample/src/main/java/io/realm/examples/realmmigrationexample/model/Migration.java#L132-L132

        schema.get("MyClass")
            .addField("new_key", int.class)
            .transform(new RealmObjectSchema.Function() {
                @Override
                public void apply(DynamicRealmObject obj) {
                    obj.setInt("new_key", Integer.valueOf(obj.getString("old_key")));
                }
            })
            .removeField("old_key")
            .addPrimaryKey("new_key")
            .renameField("new_key", "old_key");
+16

All Articles