SwiftUI crash deleting List element / conditional rendering

Using Xcode 11 beta 7, I'm getting an unexpected EXC_BAD_INSTRUCTION crash with this very simple view. It manages a list where you can add and delete items. The list starts off empty, and the crash occurs after you add any items and delete them so that the list is empty again.

The conditional rendering of something different when the list is empty (if items.count == 0...) seems to have something to do, because if I unconditionally render a List there is no crash. Yet I see no issue with the code as it is. Note that right before crashing, Text("empty") is correctly rendered.

Any ideas...?


struct TestView: View {
    @State var items: [Int] = []
    
    var body: some View {
        VStack {
            Button(action: { self.add() }) {
                Text("Add")
            }
            if items.count == 0 {
                Text("empty")
            } else {
                List {
                    ForEach(items, id: \.self) { item in
                        Text("item #\(item)")
                    }
                    .onDelete(perform: { self.delete(offset: $0.first! )})
                }
            }
        }
    }
    
    func delete(offset: Int) {
        DispatchQueue.main.async {
            self.items.remove(at: offset)
        }
    }
    
    func add() {
        DispatchQueue.main.async {
            self.items.append(self.items.count)
        }
    }
}
Answered by gbuela in 624878022
Not crashing in Xcode 12 beta
Accepted Answer
Not crashing in Xcode 12 beta
SwiftUI crash deleting List element / conditional rendering
 
 
Q