是否可以在 Realm 迁移中重命名和更改 属性 的数据类型
Is it possible to rename and change the data type of a property in a Realm migration
我目前有一个 属性 为 Double
的对象,我想将其更改为 doubles
的 List
。
根据以下代码将 price:Double
更改为 prices = List<Double>()
的最佳方法是什么?
是否可以在 Realm 迁移中重命名和更改 属性 的数据类型?如果不是,在这种情况下通常会做什么,我是否需要将 prices
视为新的 属性 并删除 属性 price
然后手动遍历所有要更改 Realm 中的项目?
迁移前 - 我目前拥有的
class Item:Object{
@objc dynamic var itemName:String = "General"
@objc dynamic var price:Double = 0
}
迁移后-迁移后我想要的
class Item:Object{
@objc dynamic var itemName:String = "General"
let prices = List<Double>()
}
迁移
以下迁移不起作用。我怎样才能修改它以使其工作?
/// Schema 1:
Realm.Configuration.defaultConfiguration = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 1) {
migration.renameProperty(onType: Item.className(), from: "price", to: "prices")
}
})
试试这个:
Realm.Configuration.defaultConfiguration = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 1) {
migration.enumerateObjects(ofType: Person.className()) { oldObject, newObject in
// Append price value to new prices list
let price = oldObject!["price"] as! Double
newObject!["prices"] = [price]
}
}
})
我目前有一个 属性 为 Double
的对象,我想将其更改为 doubles
的 List
。
根据以下代码将 price:Double
更改为 prices = List<Double>()
的最佳方法是什么?
是否可以在 Realm 迁移中重命名和更改 属性 的数据类型?如果不是,在这种情况下通常会做什么,我是否需要将 prices
视为新的 属性 并删除 属性 price
然后手动遍历所有要更改 Realm 中的项目?
迁移前 - 我目前拥有的
class Item:Object{
@objc dynamic var itemName:String = "General"
@objc dynamic var price:Double = 0
}
迁移后-迁移后我想要的
class Item:Object{
@objc dynamic var itemName:String = "General"
let prices = List<Double>()
}
迁移
以下迁移不起作用。我怎样才能修改它以使其工作?
/// Schema 1:
Realm.Configuration.defaultConfiguration = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 1) {
migration.renameProperty(onType: Item.className(), from: "price", to: "prices")
}
})
试试这个:
Realm.Configuration.defaultConfiguration = Realm.Configuration(
schemaVersion: 1,
migrationBlock: { migration, oldSchemaVersion in
if (oldSchemaVersion < 1) {
migration.enumerateObjects(ofType: Person.className()) { oldObject, newObject in
// Append price value to new prices list
let price = oldObject!["price"] as! Double
newObject!["prices"] = [price]
}
}
})