Folks,
I have finally gotten to implement a sort of lazy loading OutlineGroup. The idea is to load only the parts of the tree the OutlineGroup displays. Why? Because the underlying item (can be huge, can contain big text or spreadsheets) is externally fetched from a server, and unfortunately I have no way to ask the server to send me a 'summary' version with, say, only name and ID. :(
So the code is this:
OutlineGroup(displayTree, id: \.id, children: \.children) {item in
Text ("\(item.name) - (\(item.id))")
.onAppear {Task {await loadAllChildrenOfItemID(id: item.id)}}
.foregroundColor(item.children == nil ? .white : .blue)}
}
As you can see, the line containing the name of the item changes whether the item is a node or a leaf in the tree of items.
Lazy loading seems to work: the items are not loaded until their ‘parent’ gets displayed in the OutlineGroup after several hierarchical unfolding operations. However, at some point, I always get an inconsistent behaviour: I get blue names (meaning, the item has children), but the folding/unfolding button > does not show up in the list.
I can also mention that loading children of a given item leads to regeneration of displayTree
, which is basically a tree of structs containing only the ID and the name of the item.
The attached screenshot is typical of that curious misbehaviour. Both lines are blue, they should be preceded by the > button, but only one has it.
If I fold and unfold the parent item, I get all > as they should appear, so could this be a rat race?