SaintTail SaintTail - 3 months ago 21
Objective-C Question

Realm objective-c change schema from float to double

I want to update one property from

float
to
double
however there are no specific documentation at all about this type of migration.

Is it easy by just changing the property type in the class?
Is there some difficulty with the changing? (How?)
Or is not available at all?

Although My requirement is not that strict in the case that we can wipe that data column out or even clear all the rows of that class.

Thanks.

Answer

It's quite easy.

Say your first schema version looks like this:

// Models
class MyModel: Object {
  dynamic var prop: Float = 0
}

// Usage
let configuration = Realm.Configuration(schemaVersion: 0)
let realm = try! Realm(configuration: configuration)

Then you change the property from float to double, bump the schema version, and convert the floats from the old objects to doubles on the new object:

// Models
class MyModel: Object {
  dynamic var prop: Double = 0
}

// Usage
let configuration = Realm.Configuration(schemaVersion: 1, migrationBlock: { migration, _ in
  migration.enumerate(MyModel.className()) { oldObject, newObject in
    newObject!["prop"] = Double(oldObject!["prop"] as! Float)
  }
})
let realm = try! Realm(configuration: configuration)

This is all documented in Realm's Migration docs.