I am trying to build a Query in my view's init to select all child rows in a parent child relationship. And while my child class is defined as
@Model
final class Card {
var cardDate: Date = Date()
@Relationship(deleteRule: .nullify, inverse: \EventType.card) var eventName: EventType?
@Attribute(.externalStorage) var cardFront: Data = (UIImage(named: "frontImage")?.pngData())!
var recipient: Recipient?
init(cardDate: Date, eventName: EventType, cardFront: Data, recipient: Recipient) {
self.cardDate = cardDate
self.eventName = eventName
self.cardFront = cardFront
self.recipient = recipient
}
}
my Query is receiving "no exact matches in call to initializer
init(recipient: Recipient) {
self.recipient = recipient
_cards = Query(filter: #Predicate {
return $0.recipient == recipient
}, sort: [
Expanding the macro here -
Foundation.Predicate({
return PredicateExpressions.build_Equal(
lhs: PredicateExpressions.build_KeyPath(
root: PredicateExpressions.build_Arg($0),
keyPath: \.recipient
),
rhs: PredicateExpressions.build_Arg(recipient)
)
})
And the rest of the class here
SortDescriptor(\Card.cardDate, order: .reverse),
SortDescriptor(\Card.eventName, order: .forward),
])
}
The error is a the return PredicateExpression.build_Equal and states
/var/folders/g0/1lsdnfdn1xv6876yb5m10y340000gn/T/swift-generated-sources/@__swiftmacro_11CardTracker010ViewEventsC0V9recipientAcA9RecipientC_tcfc9PredicatefMf_.swift:2:33 Cannot convert value of type 'PredicateExpressions.Equal<PredicateExpressions.KeyPath<PredicateExpressions.Variable<Array<Card>.Element>, Recipient?>, PredicateExpressions.Value<Recipient?>>' (aka 'PredicateExpressions.Equal<PredicateExpressions.KeyPath<PredicateExpressions.Variable<Card>, Optional<Recipient>>, PredicateExpressions.Value<Optional<Recipient>>>') to closure result type 'any StandardPredicateExpression<Bool>'
How I am to setup the filter #Predicate when there is a relationship like this? The parent is recipient class which is defined as
@Model
final class Recipient {
var addressLine1: String = ""
var addressLine2: String = ""
var city: String = ""
var state: String = ""
var zip: String = ""
var country: String = ""
var firstName: String = ""
var lastName: String = ""
@Relationship(deleteRule: .cascade, inverse: \Card.recipient) var cards: [Card]?
var fullName: String {
String("\(firstName) \(lastName)")
}
init(addressLine1: String, addressLine2: String, city: String, state: String, zip: String, country: String, firstName: String, lastName: String) {
self.addressLine1 = addressLine1
self.addressLine2 = addressLine2
self.city = city
self.state = state
self.zip = zip
self.country = country
self.firstName = firstName
self.lastName = lastName
}
}