SwiftUI List with children crashes when the last item is deleted

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
Code Block SwiftUI`SwiftUI.OutlineListUpdater.init(base: SwiftUI.ListStyleDataSource<A>) -> SwiftUI.OutlineListUpdater<A>:



Are there any temporary workarounds?

Code Block import SwiftUIstruct 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()      }    }  }}

Filed a bug report #FB8965816.
Hello @ruzambo,

I copied your app into a new Xcode project and it didn't encounter the crash you specified. I can click remove button three times and it removes the last element. If I click the remove button one more time (forth time), it does crash because itemsCopy.removeLast() is being called on an empty collection.

I am also on Big Sur (20D5029f) and Xcode (20D5029f). Is your target iOS?
Hello @Tylor,
It works as intended on iOS. It crashes on macOS. Did you try that on a macOS target?

I'm seeing the same issue in macOS Monterey, Beta 1 (12.0 Beta (21A5248p)), works fine in iOS 15 beta2, but crashes in SwiftUI.OutlineListUpdater in MacOS.

I've got a feedback reply from Apple asking to re-check it on macOS 11.4 GM. Can't check it there however the issue is definitely resolved on macOS 11.5 Beta.

Hopefully they will merge this fix later. It sucks that I had to re-do whole app without Lists...

SwiftUI List with children crashes when the last item is deleted
 
 
Q