在迁移过程中添加和删除 Realm.Object
adding and removing Realm.Object during a migration
我正在进行迁移,需要从领域中删除对象并将其替换为不同的类型。
简而言之,我以前只有一个类型,现在正在创建一个层次结构,所以 BaseItem 现在需要是一个 DerivedItem。
我不确定完成此任务的最佳方法。
这是我要尝试的方法:
setSchemaVersion(kSchemaVersion, Realm.defaultPath, { migration, oldSchemaVersion in
if oldSchemaVersion == 0 {
let realm = Realm()
realm.write({ () -> Void in
old = oldObject!
if old["type"] as! Int == 1 {
let textItem = TextItem()
textItem.text = old["text"] as! String
copyBaseItemCommon(old, textItem)
realm.add(textItem)
realm.delete(newObject!)
}
})
那些添加和删除是要走的路吗?
更新:
试过这个和第 3 行的代码死锁:let realm = Realm()
有人知道进行此类迁移的技术是什么吗?
您使用的是更新后的领域吗?
基于文档..
你应该这样做>>
let realm = Realm()
realm.write
{
realm.add(theObject)
realm.delete(theObject)
}
希望对您有所帮助!
当您下载 Realm 时,您有一个示例项目。打开它,你有一个迁移的演示。
他们是这样进行迁移的:
let migrationBlock: MigrationBlock = { migration, oldSchemaVersion in
if oldSchemaVersion < 1 {
migration.enumerate(Person.className()) { oldObject, newObject in
if oldSchemaVersion < 1 {
// combine name fields into a single field
let firstName = oldObject!["firstName"] as! String
let lastName = oldObject!["lastName"] as! String
newObject?["fullName"] = "\(firstName) \(lastName)"
}
}
}
}
setDefaultRealmSchemaVersion(1, migrationBlock)
如果您查看迁移文档,您会看到在迁移中有一些创建和删除的方法:
/**
Create an `Object` of type `className` in the Realm being migrated.
:param: className The name of the `Object` class to create.
:param: object The object used to populate the object. This can be any key/value coding
compliant object, or a JSON object such as those returned from the methods in
`NSJSONSerialization`, or an `Array` with one object for each persisted
property. An exception will be thrown if any required properties are not
present and no default is set.
:returns: The created object.
*/
func create(className: String, value: AnyObject = default) -> RealmSwift.MigrationObject
/**
Delete an object from a Realm during a migration. This can be called within
`enumerate(_:block:)`.
:param: object Object to be deleted from the Realm being migrated.
*/
func delete(object: RealmSwift.MigrationObject)
并且您可以像这样在迁移块中使用它们 migration.create("TextItem", oldObject!)
。
请记住,在迁移中,您使用的是 migration
对象,而不是 Realm。
我正在进行迁移,需要从领域中删除对象并将其替换为不同的类型。
简而言之,我以前只有一个类型,现在正在创建一个层次结构,所以 BaseItem 现在需要是一个 DerivedItem。
我不确定完成此任务的最佳方法。
这是我要尝试的方法:
setSchemaVersion(kSchemaVersion, Realm.defaultPath, { migration, oldSchemaVersion in
if oldSchemaVersion == 0 {
let realm = Realm()
realm.write({ () -> Void in
old = oldObject!
if old["type"] as! Int == 1 {
let textItem = TextItem()
textItem.text = old["text"] as! String
copyBaseItemCommon(old, textItem)
realm.add(textItem)
realm.delete(newObject!)
}
})
那些添加和删除是要走的路吗?
更新:
试过这个和第 3 行的代码死锁:let realm = Realm()
有人知道进行此类迁移的技术是什么吗?
您使用的是更新后的领域吗?
基于文档..
你应该这样做>>
let realm = Realm()
realm.write
{
realm.add(theObject)
realm.delete(theObject)
}
希望对您有所帮助!
当您下载 Realm 时,您有一个示例项目。打开它,你有一个迁移的演示。
他们是这样进行迁移的:
let migrationBlock: MigrationBlock = { migration, oldSchemaVersion in
if oldSchemaVersion < 1 {
migration.enumerate(Person.className()) { oldObject, newObject in
if oldSchemaVersion < 1 {
// combine name fields into a single field
let firstName = oldObject!["firstName"] as! String
let lastName = oldObject!["lastName"] as! String
newObject?["fullName"] = "\(firstName) \(lastName)"
}
}
}
}
setDefaultRealmSchemaVersion(1, migrationBlock)
如果您查看迁移文档,您会看到在迁移中有一些创建和删除的方法:
/**
Create an `Object` of type `className` in the Realm being migrated.
:param: className The name of the `Object` class to create.
:param: object The object used to populate the object. This can be any key/value coding
compliant object, or a JSON object such as those returned from the methods in
`NSJSONSerialization`, or an `Array` with one object for each persisted
property. An exception will be thrown if any required properties are not
present and no default is set.
:returns: The created object.
*/
func create(className: String, value: AnyObject = default) -> RealmSwift.MigrationObject
/**
Delete an object from a Realm during a migration. This can be called within
`enumerate(_:block:)`.
:param: object Object to be deleted from the Realm being migrated.
*/
func delete(object: RealmSwift.MigrationObject)
并且您可以像这样在迁移块中使用它们 migration.create("TextItem", oldObject!)
。
请记住,在迁移中,您使用的是 migration
对象,而不是 Realm。