UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Dynamic Query in SwiftData is failing

Forums > Swift

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

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 -

    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

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


Ok, problem solved with this StackOverflow Help .

So now the init looks like this

   self.recipient = recipient
        let recipientID = recipient.persistentModelID // Note this is required to help in Macro Expansion
        _cards = Query(
            filter: #Predicate {$0.recipient?.persistentModelID == recipientID },
            sort: [
                SortDescriptor(\Card.cardDate, order: .reverse),
                SortDescriptor(\Card.event?.eventName, order: .forward)


Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.