I have a similar problem (until I found this question I felt like the only SwiftUI dev targeting macOS…). I think it's a SwiftUI bug. I have the Table backed by FetchedResults, and the performance issue also happens when I change the sortDescriptors.
Anyway, I am using an ugly workaround where I set a tableIsRefreshing state variable to true and replace the Table with a ProgressIndicator meanwhile. Adapted to your code this might look like this:
struct TableTest: View {
@State private var tableIsRefreshing = false
@State private var items: [Person] = (0..<500000).map { _ in Person() }
var body: some View {
VStack {
if tableIsRefreshing {
VStack {
Spacer()
ProgressView()
Spacer()
}
} else {
Table(items) {
TableColumn("Name", value: \.name)
TableColumn("Age") { person in Text(String(person.age)) }
}
}
}
.toolbar {
ToolbarItem() {
Button("Clear") {
tableIsRefreshing = true
items = []
tableIsRefreshing = false
}
}
}
}
struct Person: Identifiable {
let id = UUID()
let name: String = ["Olivia", "Elijah", "Leilani", "Gabriel", "Eleanor", "Sebastian", "Zoey", "Muhammad"].randomElement()!
let age: Int = Int.random(in: 18...110)
}
}
You might need to add a DispatchQueue.main.asyncAfter delay if this doesn't work.
I hope the bug gets fixed, please submit a bug report similar to mine: http://www.openradar.me/FB13639482