Post

Replies

Boosts

Views

Activity

SwiftData query filter on an @State var
Hey, I might have a super dumb question but I am very new to SwiftData and Swift in general. So I have the following code in a View: @State private var selectedMonth: String = "" @State private var selectedYear: String = "" @Query( filter: #Predicate<Transaction> { transaction in transaction.date.monthString == selectedMonth && transaction.date.yearString == selectedYear }, sort: \Transaction.date ) var transactions: [Transaction] My @State vars selectedMonth and selectedYear get changed whenever the user selects options from a menu. I've had trouble getting this query/filter to work, does anyone know if filtering on dynamic (@State) variables is supported in SwiftData? If so, am I missing something here? I've also tried the pattern of having the transactions array live elsewhere in a regular swift file with the @Published macro and calling an update function whenever the @State vars selectedMonth or selectedYear get updated using .onChange(of:) . Unfortunately, I've not been able to find any documentation or examples about setting up a regular swift file (not SwiftUI) to work with SwiftData (e.g. querying from it, accessing the context/container, etc) Would appreciate anyone giving tips or pointing me in the right direction. Sorry if its a noob questions, thanks so much for any help
2
4
3.1k
Dec ’23