I want use dynamically change the Predicate, so that different list can display different data. However, when I click the ‘NavigationLink’ to get in the ReminderListView()
then the app will get stuck, no responding,
HomeView code:
struct HomeView: View {
var body: some View {
NavigationStack {
HStack(spacing: 20) {
NavigationLink (destination: ReminderListView(title: "All", filter: .filterAllReminder)) {
ReminderCatagoryView(reminderCount: allReminders.count, catagoryName: "All")
}
NavigationLink (destination: ReminderListView(title: "Scheduled", filter: .filterScheduledReminder)) {
ReminderCatagoryView(reminderCount: scheduledReminders.count, catagoryName: "Scheduled")
}
} // end HStack
// ....
}
The ReminderList code is:
struct ReminderListView: View {
// ...
@Query private var reminderList: [Reminder]
init(filter: ReminderPredicate) {
let sortDescriptors: [SortDescriptor<Reminder>]
let filterPredicate: Predicate<Reminder>
switch filter {
case .filterAllReminder:
filterPredicate = #Predicate<Reminder> { $0.createDate != nil }
sortDescriptors = [SortDescriptor(Reminder.completeDate, order: .forward)]
case .filterFinishedReminder:
filterPredicate = #Predicate<Reminder> { $0.completeDate != nil}
sortDescriptors = [SortDescriptor(Reminder.completeDate, order: .forward)]
}
_reminderList = Query(filter: filterPredicate, sort: sortDescriptors)
}
}
ReminderPredicate is an enum
.
If I change filter to nil
of Query()
, then it works, this means I cannot use dynamic predicate: _reminderList = Query(filter: nil, sort: sortDescriptors)
I also tried pass predicate directly which doesn’t work: _reminderList = Query(filter: #Predicate<Reminder> { $0.createDate != nil }, sort: sortDescriptors)