swiftui - 领域 Swift 嵌入式对象。嵌入对象数据更改时视图不更新
问题描述
我试图让我的嵌入式对象在它们发生变化时自动更新我的 SwiftUI 代码。我的代码在以下条件下工作:
- 对父对象本身进行更改时。2.当我将新的嵌入对象附加到父对象时。
但是,当我更新嵌入式对象中的属性时,我的 UI 不会自动更新。
'''
class ParentObject: Object {
@objc dynamic public var primaryKey = ""
public let children = List<ChildObject>()
public override class func primaryKey() -> String? {
"primaryKey"
}
}
class ChildObject: EmbeddedObject {
let name = "Name"
}
class ParentObserver: ObservableObject {
var parentResults: Results<ParentObject>
var parents: [parent] {
parentResults.map(parent.init)
}
//The embedded objects are placed in arrays inside "parents"
{
func newParent() {
objectWillChange.send()
//My view automatically updates to reflect the new parent
}
func newChild() {
objectWillChange.send()
//My view automatically updates to reflect the new child
ParentObject.children.amend('a new child')
}
func updateChildName() {
objectWillChange.send()
//My view DOES NOT automatically updates to reflect the name change of the embedded
//object. If I exit and reenter the view the changed name show. Why does it not update
//automatically like the other cases?
ParentObject.children.first(where: { whatever id }).name = "New Name"
}
}
}
'''
解决方案
推荐阅读
- ios - 如何从 String Swift 中删除 '\u{ef}' 字符
- java - Selenium(JAVA) Grid 仅在 Windows 中启动 10 个并行浏览器
- javascript - 如何使用 javascript 编写 firebase 参考查询?
- go - 为 vscode 项目/终端设置 $GOPATH
- java - 不建议在没有服务器身份验证的情况下建立 SSL 连接
- jestjs - Jest 测试在 create-react-native-app 项目中的第一个导入语句(意外标识符)失败
- mysql - 自动递增外键?
- javascript - 将 PHP 内部的 POST 请求发送到外部 API
- python - 将表数据移动并复制到另一个表数据
- javascript - jQuery:如何让克隆的 div 始终出现在容器的底部?