I am using SwiftData & SwiftUI. This is the rough setup of my code:
struct ListItemView: View {
@Environment(\.modelContext) var modelContext
@Query
private var items: [Items]
@State private var itemToEdit: Item?
private var isEditing: Binding<Bool> {
Binding { itemToEdit != nil }
set: { _ in itemToEdit = nil }
}
var body: some View {
NavigationStack{
List(items) { item in
Text(item.name)
.onTapGesture {
itemToEdit = item
}
}
}.navigationDestination(isPresented: isEditing) {
if let item = itemToEdit {
EditView(item: item)
}
}
}
}
struct EditView: View {
var item: Item
...
}
Now, this actually works fine. If I click on the text for an item, I go into the edit view and everything works well. However, if I change the @Query to add a filter like so:
@Query(filter: #Predicate<Item>{ $0.name != ""})
private var items: [Item]
Then it no longer works. The list of items will be displayed, but if I click on any of them nothing happens and it seems like the app hangs. I also added some print statement and it seems like it is constantly creating the body of ListItemView for some reason.
On clicking on an item, I would expect that it navigates to EditView. For some reason, this is not the case but only when I add a filter predicate.