本文介绍了在领域迁移期间创建领域对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您能够在迁移期间创建领域对象吗?我想提取现有领域对象的一部分,并使用该数据创建一个新对象,但是迁移总是挂断.这是我的迁移代码
Are you able to create a realm Object during a migration? I am wanting to extract part of an existing realm object and create a new object with that data, but the migration always hangs up. Here is my migration code
private class var migrationBlock: MigrationBlock {
return { migration, oldSchemaVersion in
if oldSchemaVersion < 1 {
print("Shema Version 0")
migration.enumerate(Transaction.className(), { (oldObject, newObject) -> Void in
let oldDate = oldObject!["date"] as! NSDate
let newTransactionDate = TransactionDate()
newTransactionDate.date = oldDate
try! Realm.getRealm().write { Realm.getRealm().add(newTransactionDate, update: true) }
newObject!["_date"] = newTransactionDate
})
}
}
}
推荐答案
您可以在迁移过程中使用Migration.create(_:value:)
创建对象.
You can use Migration.create(_:value:)
to create object during migration.
它返回MigrationObject
的实例.因此,您应该使用下标为属性分配一个值.
It returns MigrationObject
's instance. So you should use subscripting to assign a value to its property.
let oldDate = oldObject!["date"] as! NSDate
let newTransactionDate = migration.create(TransactionDate.className())
newTransactionDate["date"] = oldDate
newObject!["_date"] = newTransactionDate
这篇关于在领域迁移期间创建领域对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!