我想给 FetchRequest 添加一个谓词...
not working
@SectionedFetchRequest(sectionIdentifier: \Product.manufacturer?.name,
sortDescriptors: [NSSortDescriptor(keyPath: \Product.name, ascending: true)],
predicate: NSPredicate(format: "trashed = true")) // <-- not working
var products: SectionedFetchResults<String?, Product>
.onChange
working
@SectionedFetchRequest(sectionIdentifier: \Product.manufacturer?.name,
sortDescriptors: [NSSortDescriptor(keyPath: \Product.name, ascending: true)],
// -- no predicate in FetchRequest --)
var products: SectionedFetchResults<String?, Product>
var body: some View {
List {
ForEach(products) { product in
Text(product.name)
}
Button {
testPred.toggle()
} label: {
Text("activate predicate")
}
}
}
.onChange(of: isSortedAscending) { _ in
products.nsPredicate = NSPredicate(format: "trashed = true") // <-- working
}
edit 1
我现在唯一的想法是问这是否与这些设置有关?
publicDescription.cloudKitContainerOptions = options
publicDescription.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
publicDescription.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
我发现了问题,但我现在真的要发疯了!
谓词一直有效。
但是,我已将此代码附加到视图中:
.onReceive(tabaccoName.$debounced) { query in
/// don't filter when searchbar is empty
guard !query.isEmpty else {
tabaccos.nsPredicate = nil // <-- PROBLEM
return
}
/// set filter when someone searches
tabaccos.nsPredicate = NSPredicate(format: "%K CONTAINS[cd] %@", argumentArray: [#keyPath(Tabacco.name), query])
}
而这个每次都立即将谓词设置为
nil
。