【发布时间】:2022-01-07 06:52:24
【问题描述】:
我无法弄清楚如何让 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()
}
}
【问题讨论】:
标签: swift core-data swiftui nspredicate nsfetchrequest