swift predicate 对FetchedResults有效,但对FetchRequest无效

uubf1zoe  于 2023-02-28  发布在  Swift
关注(0)|答案(1)|浏览(102)

我想向FetchRequest添加 predicate ...

如果我将其放入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将其添加到FetchedResults时,它正在工作

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)
nhaq1z21

nhaq1z211#

我发现了问题,但我现在真的要发疯了!
同品种器械一直有效。
但是,我已经将以下代码附加到视图:

.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])
        }

这一次又一次立即将 predicate 设置为nil

相关问题