Lets say I have a @Model class
like this.
@Model
class MyObject {
var isActive: Bool
var name: String
}
And I want my query results to show active objects first, in alphabetical order by name, then inactive objects after, also in alphabetical order by name.
I thought that I would be able to use a Query like this...
@Query(sort: [SortDescriptor(\MyObject.isActive), SortDescriptor(\MyObject.name)]) var objects: [MyObject]
But that gives me an error saying "No exact matches in call to initializer". So, I guess it doesn't know how to sort by Bool
even though that seems like it should be easy to do.
So, do I have to create 2 separate queries? One that filters out all of the inactive results, and one that filters out all of the active results,
@Query(
filter: #Predicate<MyObject> { object in
object.isActive
},
sort: [SortDescriptor(\MyObject.name)]
) var activeObjects: [MyObject]
@Query(
filter: #Predicate<MyObject> { object in
!object.isActive
},
sort: [SortDescriptor(\MyObject.name)]
) var inactiveObjects: [MyObject]
and then just add them both to a List
using separate ForEach
loops...
var body: some View {
List {
ForEach(activeObjects) { object in
NavigationLink(value: object) {
Text("\(object.name)")
}
}
ForEach(inactiveObjects) { object in
NavigationLink(value: object) {
Text("\(object.name)")
}
}
}
}
Or is there some easier way to do it with just one Query that I'm missing?