I'm using the latest version of RealmSwift and encountered a fatal error when running a series of linear migrations.
The issue is that a previous migration was attempting to set the value for a property that had been removed in a later version and the particular build that was running the migrations was skipping several versions. Is there a method on RealmSwift.DynamicObject
that can be used to introspect the existence of a property before attempting to set its value?
RealmSwift.Object
has an objectSchema
property which describes the schema being used for that specific object. You can use this to check for the presence of a property with 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)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With