gpt4 book ai didi

swift Realm : After writing transaction reference set to nil

转载 作者:行者123 更新时间:2023-11-28 06:49:45 26 4
gpt4 key购买 nike

我有以下代码

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)
}

问题:为什么 familyMember.belongsToFamily 在 Realm 写入交易后设置为 nil?

最佳答案

这是预期的行为。在实际访问属性之前,Realm 不会复制任何数据。当访问属性时,Realm 直接从它的文件中获取数据。所以 Realm 不会将任何数据存储到它的 ivar 中。此外,Realm 对象在持久化时会更改为另一个类。这就是为什么您无法通过调试器看到任何值并且在提交后将所有值清零的原因。

所以如果你想调试对象的值,你可以在调试控制台使用po命令,或者只使用print()方法。

另见 https://realm.io/docs/swift/latest/#debugging

Debugging apps using Realm’s Swift API must be done through the LLDB console.

Note that although the LLDB script installed via our Xcode Plugin allows inspecting the contents of your Realm variables in Xcode’s UI, this doesn’t yet work for Swift. Instead, those variables will show incorrect data. You should instead use LLDB’s po command to inspect the contents of data stored in a Realm.

另见:https://github.com/realm/realm-cocoa/issues/2777

关于 swift Realm : After writing transaction reference set to nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35235031/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com