我正在使用最新版本的RealmSwift,並在運行一系列線性遷移時遇到致命錯誤。在Realm中運行線性遷移時檢查屬性是否存在
問題是,以前的遷移嘗試爲在較高版本中刪除的屬性設置值,並且運行遷移的特定構建跳過了多個版本。 RealmSwift.DynamicObject
有沒有一種方法可以用來在嘗試設置其值之前反思屬性的存在?
我正在使用最新版本的RealmSwift,並在運行一系列線性遷移時遇到致命錯誤。在Realm中運行線性遷移時檢查屬性是否存在
問題是,以前的遷移嘗試爲在較高版本中刪除的屬性設置值,並且運行遷移的特定構建跳過了多個版本。 RealmSwift.DynamicObject
有沒有一種方法可以用來在嘗試設置其值之前反思屬性的存在?
RealmSwift.Object
有一個objectSchema
屬性,它描述了用於該特定對象的模式。你可以用它來檢查是否存在object.objectSchema.properties.contains { $0.name == "propName" }
的財產。
extension Migration {
func hadProperty(onType typeName: String, property propertyName: String) -> Bool {
var hasPropery = false
self.enumerateObjects(ofType: typeName) { (oldObject, _) in
hasPropery = oldObject?.objectSchema.properties.contains(where: { $0.name == propertyName }) ?? false
return
}
return hasPropery
}
func renamePropertyIfExists(onType typeName: String, from oldName: String, to newName: String) {
if (hadProperty(onType: typeName, property: oldName)) {
renameProperty(onType: typeName, from: oldName, to: newName)
}
}
}