One of my apps has been crashing on multiple Sequoia betas, something that never happened on prior macOS versions, and I’ve finally narrowed it down to a smaller reproducible case which I've attached to bug report #FB14473269.
The narrowed case is fairly trivial. A table containing data is filtered by typing into a text field which narrows the bound content by finding only text with matching substrings. It's a single basic view the source for which is included in its entirety below. To reproduce the crash:
- Compile and run
- Type “five” in the search text
- If this doesn’t immediately crash it with the first keystroke (there seems to be some kind of race condition so it may vary on different hardware configurations), delete a character at a time and repeat as necessary. Typing “one” on my system doesn’t crash the app, but deleting backwards to just “o” immediately crashes.
The backtrace is entirely within SwiftUI. This works flawlessly on Sonoma which is where I've been building and testing the app where this problem was first discovered, and it's completely unusable up through beta 5 of Sequoia.
It's hard to believe nobody else is encountering this. Any suggestions?
import SwiftUI
class Item: Identifiable {
var id: String
init(_ id: String) {
self.id = id
}
}
struct ContentView: View {
@State var allItems = [ Item("One"), Item("Two"), Item("Three"), Item("Four"), Item("Five"), Item("Six"), Item("Seven"), Item("Eight"), Item("Nine"), Item("Ten") ]
@State var filteredItems = [Item]()
@State var selectedItems = Set<Item.ID>()
@State var text: String = ""
func filter() {
if text == "" {
filteredItems = allItems
} else {
filteredItems = allItems.filter {
$0.id.localizedCaseInsensitiveContains(text)
}
}
}
var body: some View {
VStack {
TextField("Search:", text: $text)
.padding()
.onChange(of: text) {
filter()
}
.onAppear {
filter()
}
Table($filteredItems, selection: $selectedItems) {
TableColumn("Name") { item in
Text(item.id)
}
}
}
}
}
#Preview {
ContentView()
}
@KateS I wasn't able to reproduce the issue using the Latest beta. Could you test and verify using the latest beta build.
Also I would suggest you use the searchable
modifier instead.
Here are a few resources to help you get started:
struct ContentView: View {
@State private var allItems = [ Item("One"), Item("Two"), Item("Three"), Item("Four"), Item("Five"), Item("Six"), Item("Seven"), Item("Eight"), Item("Nine"), Item("Ten") ]
@State private var filteredItems = [Item]()
@State private var selectedItems = Set<Item.ID>()
@State private var searchText: String = ""
func filter() {
if searchText == "" {
filteredItems = allItems
} else {
filteredItems = allItems.filter {
$0.id.localizedCaseInsensitiveContains(searchText)
}
}
}
var body: some View {
NavigationStack {
Table($filteredItems, selection: $selectedItems) {
TableColumn("Name") { item in
Text(item.id)
}
}
}
.onChange(of: searchText, initial: true) { _,_ in
filter()
}
.searchable(text: $searchText, placement: .sidebar)
}
}