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()
}
}
}
}
5
0
1.9k