I'm having a strange issue with objects in Realm. When I fetch an object from Realm database sometimes some of its fields (it only happens on RealmOptionals) are nil after accessing them.
But it's really weird because if I put a breakpoint before the access to that fields and I execute po object.field on lldb the nil fields are gone after that.
I have seen that maybe I have forgot to set that fields as ´dynamic´ but in my case, and following Realm docs, I have to use RealmOptional for optional booleans.
Does anyone know what's the issue here?
Thanks in advance.
EDIT: The method that I used to retrieve and object from Realm is:
func getObject(atPosition position:Int)->Object{
let objects = realm.objects(Object.self)
return objects[position]
}
I'm not exactly sure what you're asking. If a Realm property is marked optional, then its initial value will be nil as expected.
You're correct in that you need to use let propertyValue = RealmOptional<Bool>() instead of dynamic as normal boolean data types can't express a nil value on their own. So this is an exception to the dynamic properties rule.
Also be aware you need to use optional.value to set an optional Bool's value:
class Dog: Object {
dynamic var name = ""
dynamic var age = 0
let vaccinated = RealmOptional<Bool>()
}
let realm = try! Realm()
let myDog = Dog()
myDog.name = "Sherlock Bones"
myDog.age = 4
myDog.vaccinated.value = true
try! realm.write {
realm.add(myDog)
}
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