How to use a @FetchRequest with the new searchable modifier in SwiftUI?

I would not overcomplicate it with additional bindings. Why not only to use the onChange modifier on the searchText.

struct ContentView: View {
    @FetchRequest(sortDescriptors: [SortDescriptor(\Quake.time, order: .reverse)])
    private var quakes: FetchedResults<Quake>
    
    @State private var searchText = ""
    
    var body: some View {
        List(quakes) { quake in
            QuakeRow(quake: quake)
        }
        .searchable(text: $searchText)
        .onChange(of: searchText) { newValue in
            quakes.nsPredicate = newValue.isEmpty ? nil : NSPredicate(format: "place CONTAINS %@", newValue)
        }
    }
}

Leave a Comment