简体   繁体   English

领域Objective-C将架构从float更改为double

[英]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. 我想将一个属性从float更新为double但是根本没有关于此类迁移的特定文档。

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: 然后,将属性从float更改为double,更改模式版本,然后将旧对象的float转换为新对象的double:

// 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 . 所有这些都记录在Realm的Migrations文档中

Change your property type from float to double and run 将属性类型从float更改为double并运行

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 这通过调用doubleValue执行从float到double的转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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