SwiftUI List crashes when removing a selected item

Hi,

I have a severe problem with the SwiftUI List view in Xcode 12 (beta).

When a List item, which is selected, is removed, the List crashes every time.

"[General] Row 2 out of row range [0-1] for rowViewAtRow:createIfNeeded:"

Looks like a bug in SwiftUI to me.
What can I do to prevent the crash? I've tried several things already, but with no success. I'm stuck with this problem since 3 days now...

Example code:
Code Block //
// Example to reproduce bug
// * Select no item or other than last item and press button: selection is reset, last item is removed, no crash
// * Select last list item and press button "Delete last item" => Crash
//
import SwiftUI
class MyContent: ObservableObject {
    @Published var items: [String] = []
    @Published var selection: Set<String> = Set()
    
    init() {
        for i in 1...5 {
            self.items.append(String(i))
        }
    }
}
struct MyView: View {
    @ObservedObject var content: MyContent = MyContent()
    var body: some View {
        VStack {
            List(content.items, id: \.self, selection: $content.selection) {
                item in
                Text("\(item)")
            }
            Button("Delete last item", action: {
                if content.items.count > 0 {
                    content.selection = Set()  // reset selection
                    var newItems = Array(content.items)
                    newItems.removeLast()
                    content.items = newItems
                }
            })
        }
    }
}


Accepted Reply

Re-tested after installing MacOS 11.0 Beta 7 (20A5374g).

The example code doesn’t crash any more, the bug seems to be fixed.

Thanks to all for testing and so giving me the hint, that it's a MacOS beta bug. :-)

Replies

Sorry, but I cannot reproduce the same issue with your code. (Tested with Xcode 12 beta 6.)

Can you find what's affecting and show the context to reproduce?

To reproduce, you have to select the last item in the list and hit the delete button. In my environment, it crashed 100% sure.

I'm using Xcode 12 beta6 on a Mac Book Air running the latest MacOS Big Sur Beta (build 20A5364e).
Did another check: I created a new, clean MacOS App project in Xcode, put the example code in ContentView.swift and exchanged the ContentView() in the App by MyView(). Then choose build profile MacOS and start. After that select the last entry and hit the Delete button and Boom, it crashes...

I created a new, clean MacOS App project in Xcode

Thanks for adding such important info, which was missing in the opening post.

I'm using Xcode 12 beta6 on a Mac Book Air running the latest MacOS Big Sur Beta (build 20A5364e).

Unfortunately, I do not have a Mac with Big Sur, so I cannot test and run it on Big Sur.
But as far as I tried on Catalina, your code runs without any problems,

I too think this is a bug of SwiftUI runtime on Big Sur, but I cannot help finding some workarounds for it.
(Better send a bug report soon.)

Hope someone (maybe yourself) can find a workaround soon.
Did 2 Checks now:
  1. MacOS 11 beta (20A5364e) + Xcode 11.7 => Crash

  2. MacOS 10.15.6 + Xcode 11.7 => no Crash

The bug is clearly in MacOS 11 beta...

Thanks for helping, even when there's no solution yet, but it's clear now, that it's not my fault... :-)
Filed Apple bug report (FB8680646)...
Post not yet marked as solved Up vote reply of h725 Down vote reply of h725
Re-tested after installing MacOS 11.0 Beta 7 (20A5374g).

The example code doesn’t crash any more, the bug seems to be fixed.

Thanks to all for testing and so giving me the hint, that it's a MacOS beta bug. :-)
The crash remains if there are children in items and you delete the last item.