Migrating realm, adding a RealmProperty<Float?>


I’m using the same realm definition in my Android and iOS app and while on Android, the migration block easily allows adding new optional fields, I struggle to find a solution for how to achieve that in Swift.

This is how my realm object looks like, the last property is the one I want to add in the new version. It does not automatically get added to the table like @Persisted properties do. It cannot be tagged with @Persisted either. How do I solve this problem and add the column to the table?

class RelevantDO: Object {
    @Persisted var name: String? = ""
    @Persisted var count: Int = 0
    let percentage = RealmProperty<Float?>()

My migration block in Java looks like this:

        if (version == 53) {
                    .addField("percentage", Float.class)
                    .transform(obj -> obj.set("percentage", null));

A couple of things

  1. From the docs

New in version 10.10.0 : The @Persisted declaration style replaces the @objc dynamic , RealmOptional , and RealmProperty

So you shouldn’t be using RealmProperty at this point.

Also additive changes do not require a migration at all, only destructive changes.

  1. Adding a float would just be

class RelevantDO: Object {

@Persisted var name: String? = ""
@Persisted var count: Int = 0
@Persisted var percentage = 0.0 //for example. Or @Persisted var percentage: Float

So to be 100% exact, it’s just

@Persisted var percentage: Float? = null


Yes! And that’s an OPTIONAL option as well.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.