SwiftUI List: One row appears instead of two when quickly tapping add row button twice

Originator:indiekiduk
Number:rdar://FB8991416 Date Originated:2021-02-05
Status:Open Resolved:No
Product:SwiftUI Product Version:iOS 14.5b1
Classification:Bug Reproducible:Always
 
Hi I noticed a glitch in the UITableView backed SwiftUI List on iOS - if there is an add button to add a new row animated, if quickly tapping the button then the list only shows 1 row and not the expected 2. Perhaps the UITableView cell animations are tripping over themselves. I have attached a sample app demonstrating the problem, please follow these steps to recreate it or watch the attached screencast.

1. Open Project.
2. Launch in iPhone 12 mini simulator.
3. Click the + button twice quickly.

What expected:
2 rows should appear in list.

What happens:
Only 1 row appears.

Bonus:
Do a swipe gesture on the row, then the table updates and the missing second row appears!

Tested on:
Xcode 12.5 beta 1
iOS Simulator iOS 14.5 (18E5140i) Model: iPhone 12 mini

But also present on earlier versions.




import SwiftUI

struct Item: Identifiable {
    let id = UUID()
    let timestamp = Date()
}

struct ContentView: View {
    @State var items = [Item]()
    
    var body: some View {
        NavigationView {
            List {
                ForEach(items) { item in
                    Text("Item at \(item.timestamp, formatter: itemFormatter)")
                }
                .onDelete(perform: deleteItems)
            }
            .toolbar {
                ToolbarItem(placement: .navigation) {
                    EditButton()
                }
                ToolbarItem(placement: .primaryAction) {
                    Button(action: addItem) {
                        Label("Add Item", systemImage: "plus")
                    }
                }
                ToolbarItem(placement: .principal) {
                    Button(action: deleteAllItems) {
                        Text("Delete All")
                    }
                }
            }
        }
    }
    
    private func addItem() {
        withAnimation {
            items.append(Item())
        }
    }
    
    private func deleteItems(offsets: IndexSet) {
        withAnimation {
            items.remove(atOffsets: offsets)
        }
    }
    
    private func deleteAllItems() {
        withAnimation {
            items.removeAll()
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

private let itemFormatter: DateFormatter = {
    let formatter = DateFormatter()
    formatter.dateStyle = .short
    formatter.timeStyle = .medium
    return formatter
}()

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!