我無法找出如何使SwiftUI@FetchRequest使用僅存盤在一個布林值的謂詞UserDefaults是true。
問題是FetchedResults當值改變時 不會改變showAvailable。目前我必須重新啟動應用程式才能查看showAvailable基于價值的結果。
struct ContentView: View {
@AppStorage("showAvailable") private var showAvailable : Bool = false
@FetchRequest private var list: FetchedResults<Item>
init() {
@AppStorage("showAvailable") var showAvailable : Bool = false
_list = FetchRequest(
sortDescriptors: [],
predicate: showAvailable ? NSPredicate(format: "parent == nil") : nil,
animation: .default
)
}
var body: some View {
Button("Cancel") {
showAvailable.toggle()
}
}
uj5u.com熱心網友回復:
我將在 Button 操作中使用新謂詞更新獲取請求,并且該更新應觸發再次執行的請求
struct ContentView: View {
@AppStorage("showAvailable") private var showAvailable : Bool = false
@FetchRequest(sortDescriptors: [],
predicate: NSPredicate(value: true)
animation: .default)
private var list: FetchedResults<Item>
var body: some View {
Button("Cancel") {
showAvailable.toggle()
updatePredicate()
}
.onAppear {
updatePredicate()
}
}
private func updatePredicate() {
if showAvailable {
list.nspredicate = NSPredicate(format: "parent == nil")
} else {
list.nspredicate = NSPredicate(value: true)
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/370546.html
標籤:迅速 核心数据 迅捷 谓词 nsfetchrequest
