Hello,
I narrowed down the problem and created a clean project. The code is below.
I found similar issue on forum, it was fixed there eventually for a regular list. However it wasn't fixed for a list with children.
Reproduces 100% of time, faced on macOS 11.1, I'm on "11.2 Beta (20D5029f)" currently.
Xcode Version 12.3 (12C33)
Run the project, click the remove button 3 times - it crashes.
Try and remove "children" part of the list, it stops crashing.
The crash happens at
SwiftUI`SwiftUI.OutlineListUpdater.init(base: SwiftUI.ListStyleDataSource<A>) -> SwiftUI.OutlineListUpdater<A>:
Are there any temporary workarounds?
import SwiftUI
struct Item: Identifiable {
let id: UUID
let name: String
let children: [Item]?
init(id: UUID = UUID(), name: String = String(UUID().uuidString.prefix(6)), children: [Item]? = nil) {
self.id = id
self.name = name
self.children = children
}
}
class ViewModel: ObservableObject {
@Published var items: [Item] = ViewModel.generateRandom()
func removeLast() {
var itemsCopy = items
itemsCopy.removeLast()
items = itemsCopy
}
func generateNew() {
items = ViewModel.generateRandom()
}
static func generateRandom() -> [Item] {
[
.init(children: [.init(), .init()]),
.init(children: [.init(), .init()]),
.init(children: [.init(), .init()]),
]
}
}
struct ContentView: View {
@StateObject var viewModel: ViewModel = ViewModel()
var body: some View {
VStack {
List(viewModel.items, id: \.id, children: \.children) { item in
VStack {
Text(item.name).padding()
Divider()
}
}
Button("generate new") {
viewModel.generateNew()
}
Button("remove last") {
viewModel.removeLast()
}
}
}
}
Post
Replies
Boosts
Views
Activity
I need to display the following data as a list with groups
Item 00
Item 01
		- Item 10
				 - Item 20
				 - Item 21
		- Item 11 Item 02
However if Item 10 is collapsed Item 11 is shown as Item 02.
What am I doing wrong? Unique IDs are set.
You can see it clearly on screenshots:
Corrupted
https://github.com/kmalyshev/ListProblemExample/blob/main/images/Screenshot1.png
Correct
https://github.com/kmalyshev/ListProblemExample/blob/main/images/Screenshot2.png
The code:
import SwiftUI
struct ContentView: View {
let data = Item.getSampleData()
var body: some View {
List(data, id: \.id, children: \.children) { item in
Text(item.name)
}.listStyle(SidebarListStyle())
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct Item: Codable {
let id: UUID
let name: String
let children: [Item]?
init(id: UUID = UUID(), name: String, children: [Item]? = nil) {
self.id = id
self.name = name
self.children = children
}
static func getSampleData() -> [Item] {
[
Item(name: "Item 00"),
Item(
name: "Item 01",
children: [
Item(
name: "Item 10",
children: [
Item(name: "Item 20"),
Item(name: "Item 21"),
]
),
Item(name: "Item 11"),
]
),
Item(name: "Item 02"),
]
}
}
I created a sample project that shows the problem.
https://github.com/kmalyshev/ListProblemExample
I tried it on macOS 11.0.1 and 11.1 Beta
Updated Xcode to 12.3 - same result. OutlineGroup and List with ForEach using recursive view give the same result.
Any help is appreciated.