Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm object property misses its value, but I can see it when printing the whole object

Tags:

swift

realm

I stumbled upon a weird thing when trying to fetch an object from my Realm (iOS, Swift, Realm version 0.98.2)

print("speaker:")
print(RealmProvider.appRealm.objects(FavoriteSpeaker).first!)

Correctly dumps my object in the console:

speaker:
FavoriteSpeaker {
    name = Ashley Nelson-Hornstein;
}

But when I try to get the name property's value:

print("speaker name:")
print(RealmProvider.appRealm.objects(FavoriteSpeaker).first!.name)

I get an empty string 🤔

speaker name:

The four lines are together in my model's init method


Update 1: I found an answer that suggests that you merely don't see the values when printed in the Console: Realm object is missing all properties except primaryKey but I also tried displaying the name property via an alert view and that is also empty.


Update 2: Just to make sure that everything happens sequentially and on the same thread I did this:

let favorite1 = FavoriteSpeaker()
favorite1.name = "Debbie Downer"

try! RealmProvider.appRealm.write {
    RealmProvider.appRealm.deleteAll()
    RealmProvider.appRealm.add(favorite1)
}

print("speaker:")
print(RealmProvider.appRealm.objects(FavoriteSpeaker.self).first!)

print("speaker name:")
print(RealmProvider.appRealm.objects(FavoriteSpeaker.self).first!.name)

But the result is the same - printing name prints an empty string

like image 430
Marin Todorov Avatar asked Feb 23 '16 11:02

Marin Todorov


1 Answers

The name property is probably not declared as dynamic, which leads to it reading the nil value stored on the object itself rather than reading the data from the Realm.

like image 178
Thomas Goyne Avatar answered Nov 10 '22 11:11

Thomas Goyne