简体   繁体   中英

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.

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 .

Change your property type from float to double and run

RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];

    config.migrationBlock = ^(RLMMigration *migration, uint64_t oldSchemaVersion) {

        if(oldSchemaVersion < 26) {
            [migration enumerateObjects:TransformationParameters.className block:^(RLMObject * _Nullable oldObject, RLMObject * _Nullable newObject) {
                newObject[@"translationX"] = @([oldObject[@"translationX"] doubleValue]);

            }];
        }
    };

This performs the conversion from float to double by the call to doubleValue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM