Swift Realm: after writing a transaction reference to zero

I have the following code

class Family: Object { dynamic var name = "" var members = List<FamilyMember>() } class FamilyMember: Object { dynamic var name = "" dynamic var belongsToFamily: Family? } let realm = try! Realm() let familyMember = FamilyMember() familyMember.name = "NameExample" let newFamily = Family() newFamily.name = "Familyname" try! realm.write { newFamily.members.append(familyMember) familyMember.belongsToFamily = newFamily realm.add(newFamily) realm.add(familyMember) } 

QUESTION: Why is familyMember.belongsToFamily set to nil after a Realm transaction?

+1
source share
2 answers

It was meant to be behavior. Realm does not copy data until the properties are actually accessed. When accessing properties, Realm extracts data directly from its file. So Realm does not store data in its ivar. In addition, the Realm object will be changed to another class when it is saved. Thus, you cannot see any values ​​through the debugger and display all the values ​​after they are completed.

So, if you want to debug the value of objects, you can use the po command in the debug console or just use the print() method.

See also https://realm.io/docs/swift/latest/#debugging

Debugging applications using the Realms Swift API must be done through the LLDB console.

Please note that although the LLDB script installed through our Xcode Plugin allows you to check the contents of your Realm variables in the Xcodes user interface, this still does not work for Swift. Instead, these variables will show invalid data. Instead, you should use the LLDBs po command to check the contents of the data stored in Realm.

And look also: https://github.com/realm/realm-cocoa/issues/2777

+1
source

You can try to delete the line

 realm.add(newFamily) 

from your recording unit?

0
source

Source: https://habr.com/ru/post/1243200/


All Articles