I have a list in SwiftUI in (ListView) that pulls data from Core Data. I would like to filter it dynamically. I have a fetchRequest like this:
init(filter: Bool) {
fetchRequest = FetchRequest<Transaction>(entity: Transaction.entity(), sortDescriptors: [], predicate: NSPredicate(format: "income = %d", filter))
}
Transaction is a Core Data Entity with an Attribute income (Bool).
In ContentView I am calling ListView like this:
ListView(filter: incomeTypeFilter)
And I have 3 buttons to change filter (true or false which stands for true=income, false=expenses):
Button("Show income") {
self.incomeTypeFilter = true
}
Button("Show expenses") {
self.incomeTypeFilter = false
}
Button("Show ALL") {
// I don't know how to show ALL items in the list. Both true and false.
}
Buttons to show income and expenses work. They change self.incomeTypeFilter to be true or false. But how to change predicate to show ALL items? Both true and false?
P.s. My code is based on this tutorial: Dynamically filtering @FetchRequest with SwiftUI