SwiftUI 2.0 如何在添加新子项时及时刷新列表?

SwiftUI 2.0 How to refresh List in time when add new subitems?

大家都知道,从SwiftUI 2.0开始,List就支持递归数据的分层显示了。

我的递归数据结构Tree定义如下:

class Tree<Value: Hashable>: Hashable {
    static func == (lhs: Tree<Value>, rhs: Tree<Value>) -> Bool {
        lhs.value == rhs.value
    }
    
    func hash(into hasher: inout Hasher) {
        hasher.combine(value)
    }
    
    let value: Value
    var children: [Tree]? = nil
    
    init(value: Value, children: [Tree]? = nil){
        self.value = value
        self.children = children
    }
}

我的模型定义如下:

class Model: ObservableObject {
    @Published var trees = [Tree<String>]()
}

最后是我的 TreeView 定义:

struct TreeView: View {
    
    @StateObject var model = Model()
    
    var body: some View {
        VStack {
            HStack {
                Spacer()
                Button("+"){
                    let new = Tree(value: String(UUID().uuidString.prefix(16)))
                    // List can be refreshed in time
                    model.trees.append(new)
                }
            }
            .padding()
            
            Text("Tree")
                .font(.title)
            
            Spacer()
            
            List {
                ForEach(model.trees, id: \.value){ section in
                    Section(header: HStack {
                        Text("\(section.value)")
                        Spacer()
                        Button("+"){
                            let new = Tree(value: String(UUID().uuidString.prefix(8)))
                            var new_children: [Tree<String>]
                            if let children = section.children {
                                new_children = children
                                new_children.append(new)
                            }else{
                                new_children = [new]
                            }
                            
                            // List can't be refreshed in time
                            section.children = new_children
                        }
                        .buttonStyle(BorderlessButtonStyle())
                    }){
                        OutlineGroup(section.children ?? [], id: \.value, children: \.children){tree in
                            HStack {
                                Text(tree.value)
                                Spacer()
                                Button("+"){
                                    let new = Tree(value: String(UUID().uuidString.prefix(8)))
                                    var new_children: [Tree<String>]
                                    if let children = tree.children {
                                        new_children = children
                                        new_children.append(new)
                                    }else{
                                        new_children = [new]
                                    }
                                    // List can't be refreshed in time
                                    tree.children = new_children
                                }
                                .buttonStyle(BorderlessButtonStyle())
                            }
                        }
                    }
                }
            }
            .listStyle(SidebarListStyle())
        }
    }
}

struct ProductListView_Previews: PreviewProvider {
    static var previews: some View {
        TreeView()
    }
}

当我向model.trees数组中添加一个新的Tree时,List可以及时刷新,但是当我向trees数组中的一个元素添加一个新的Tree时(例如:model.trees [0]), 列表无法刷新

添加子树时如何及时刷新列表?谢谢!

作为每个“+”按钮插入的第一个命令:
model.objectWillChange.send() - 然后就可以了。