init(filter:sort:animation:)
Create a query with a predicate, and a list of sort descriptors.
Declaration
@MainActor @preconcurrency init(filter: Predicate<Element>? = nil, sort descriptors: [SortDescriptor<Element>] = [], animation: Animation) where Result == [Element]Parameters
- filter:
A predicate on
Element - descriptors:
Sort orders for the result.
- animation:
The animation to use for user interface changes that result from changes to the fetched results.
Discussion
Use Query within a view by wrapping the variable for the query’s result:
struct RecipeList: View {
// Favorite recipes sorted by date of creation
@Query(
filter: #Predicate { $0.isFavorite == true },
sort: [SortDescriptor(\.dateCreated)]
)
var favoriteRecipes: [Recipe]
var body: some View {
List(favoriteRecipes) { RecipeDetails($0) }
}
}