How to migrate a field from a boxed version to an unboxed version

66 views Asked by At

I have a field that is a Double. I want to change it to a double field. Here's the code I have set up to do that:

schema.get("Item")
        .addField("mOriginalWeightUnboxed",double.class)
        .transform(new RealmObjectSchema.Function() {
            @Override
            public void apply(DynamicRealmObject obj) {
                Double boxedWeight=obj.get("mOriginalWeight");
                if (boxedWeight==null)
                    boxedWeight=0.0;
                obj.setDouble("mOriginalWeightUnboxed",boxedWeight.doubleValue());
            }
        })
        .removeField("mOriginalWeight")
        .renameField("mOriginalWeightUnboxed","mOriginalWeight");

Would this be a recommended way of doing that?

1

There are 1 answers

3
EpicPandaForce On BEST ANSWER

Double and double are the same for Realm, except double is not nullable.

So if you want to make a Double into a double, then just make the field @Required, and add the annotation for it via migration as well.

schema.get("Item")
    .setNullable("mOriginalWeight", false);