如何在 SwiftUI 中将 @FetchRequest 与新的可搜索修饰符一起使用?
How to use a @FetchRequest with the new searchable modifier in SwiftUI?
是否可以将新的 searable
与 @FetchRequest
结合使用?
我有这样的代码:
struct FooListView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \Foo.name, ascending: true)],
animation: .default)
private var items: FetchedResults<Foo>
@State var searchText = ""
var body: some View {
NavigationView {
List {
ForEach(items) { item in
NavigationLink(destination: FooView(Foo: item)) {
Text(item.wrappedName)
}
}
.onDelete(perform: deleteItems)
}
.searchable(text: $searchText)
.navigationTitle("Foos")
}
}
}
在这个简单的例子中,我想使用 searchText
来欺骗我的 FetchedResults
。
WWDC 2021 将核心数据并发带到 Swift,SwiftUI 就在 21:33
分钟左右有一个很好的例子
https://developer.apple.com/wwdc21/10017
struct ContentView: View {
@FetchRequest(sortDescriptors: [SortDescriptor(\Quake.time, order: .reverse)])
private var quakes: FetchedResults<Quake>
@State private var searchText = ""
var query: Binding<String> {
Binding {
searchText
} set: { newValue in
searchText = newValue
quakes.nsPredicate = newValue.isEmpty
? nil
: NSPredicate(format: "place CONTAINS %@", newValue)
}
}
var body: some View {
List(quakes) { quake in
QuakeRow(quake: quake)
}
.searchable(text: query)
}
}
我不会用额外的绑定使它过于复杂。为什么不只在 searchText 上使用 onChange 修饰符。
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)
}
}
}
是否可以将新的 searable
与 @FetchRequest
结合使用?
我有这样的代码:
struct FooListView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \Foo.name, ascending: true)],
animation: .default)
private var items: FetchedResults<Foo>
@State var searchText = ""
var body: some View {
NavigationView {
List {
ForEach(items) { item in
NavigationLink(destination: FooView(Foo: item)) {
Text(item.wrappedName)
}
}
.onDelete(perform: deleteItems)
}
.searchable(text: $searchText)
.navigationTitle("Foos")
}
}
}
在这个简单的例子中,我想使用 searchText
来欺骗我的 FetchedResults
。
WWDC 2021 将核心数据并发带到 Swift,SwiftUI 就在 21:33
分钟左右有一个很好的例子https://developer.apple.com/wwdc21/10017
struct ContentView: View {
@FetchRequest(sortDescriptors: [SortDescriptor(\Quake.time, order: .reverse)])
private var quakes: FetchedResults<Quake>
@State private var searchText = ""
var query: Binding<String> {
Binding {
searchText
} set: { newValue in
searchText = newValue
quakes.nsPredicate = newValue.isEmpty
? nil
: NSPredicate(format: "place CONTAINS %@", newValue)
}
}
var body: some View {
List(quakes) { quake in
QuakeRow(quake: quake)
}
.searchable(text: query)
}
}
我不会用额外的绑定使它过于复杂。为什么不只在 searchText 上使用 onChange 修饰符。
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)
}
}
}