I have three models in SwiftData – a parent Event related to Group and Item. I have a list of all Items by Group that belong to a specific Event. But I can’t figure out how to list the Items that belong to a specific Event that aren’t associated with a Group.
MODELS
@Model
class Event: Identifiable {
var id = UUID()
var name: String = ""
@Relationship(deleteRule: .cascade)
var groups: [Group]?
var items: [Item]?
}
@Model
class Group: Identifiable {
var id = UUID()
var name: String = ""
@Relationship(inverse: Event.groups) var group: Group?
@Relationship(deleteRule: .cascade) var items: [Item]?
}
@Model
class Item: Identifiable {
var id = UUID()
var name: String = ""
var notes: String = ""
@Relationship(inverse: Event.items) var event: Event?
@Relationship(inverse: Group.items) var group: Group?
}
I’m using a query to assign the Groups without an Event to a variable and then building a list off that:
@Query(filter: #Predicate<Group>{ ($0.event == nil) }) private var looseGroups: [Group]
But when I try that to identify Items without a Group, I am having trouble limiting them to a specific Event. If I use the same query, I get all items across all Events, rather than the specific one I want.
I’ve tried a sort of cascade logic first getting items where Group == nil and then getting event.items:
if item.groups == nil {
ForEach(event.items!.sorted(by: {$0.status < $1.status}))
But this causes errors further up in the first ForEach in the list:
Static method 'buildExpression' requires that 'Section<EmptyView, TupleView<(some View, some View)>, EmptyView>' conform to 'AccessibilityRotorContent'
Is there a way to do this sort of stacked logic to create a list of the Items in a specific Event but not associated with any Groups?
Thank you.