# Complex Predicates in SwiftData

**URL:** <https://forums.swift.org/t/complex-predicates-in-swiftdata/73565>\
**Category:** Using Swift\
**Tags:** swiftdata\
**Created:** [July 30, 2024, 3:30pm UTC](https://forums.swift.org/t/complex-predicates-in-swiftdata/73565 "2024-07-30T15:30:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![natebird](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/natebird/32/596_2.png) [@natebird](https://forums.swift.org/u/natebird)\
**Post date:** [July 30, 2024, 3:30pm UTC](https://forums.swift.org/t/complex-predicates-in-swiftdata/73565/1 "2024-07-30T15:30:19Z")

</div>

I'm running into an issue trying to combine Predicates to be used with SwiftData.

This [PR](https://github.com/apple/swift-foundation/pull/343) by @jmschonfeld adds the ability to combine predicates together by defining predicate A, predicate B and then predicate C can combine them together.

The issue I'm running into is that this doesn't seem to work for my case. I have a Quote model that has relationships defined for Authors and Tags - both are many to many relationships.

What I would like to do is provide a SwiftData query to see if a search term contains the quote.content, the quote.authors.name, to the quote.tags.name.

Following the example in the PR above I created the following:

```swift
@Query private var quotes: [Quote]
...
let contentContainsTerm = #Predicate<Quote> { $0.content.localizedStandardContains(searchText) }
let authorNameContainsTerm = #Predicate<Author> { $0.name.localizedStandardContains(searchText) }
let tagNameContainsTerm = #Predicate<Tag> { $0.name.localizedStandardContains(searchText) }

let filter = #Predicate<Quote, Author, Tag> {
    contentContainsTerm.evaluate($0) ||
    authorNameContainsTerm.evaluate($1)) ||
    tagNameContainsTerm.evaluate($2)

 _quotes = Query(filter: filter, sort: [sortDescriptor])

```

This produces the error: `Cannot convert value of type 'Predicate<Quote, Author, Tag>' to expected argument type 'Predicate<Array<Quote>.Element>' (aka 'Predicate<Quote>')`

I understand that the Types are not comparable but I'm not sure how to create the predicates with the Quote type and reference the relationships. Any guidance would be appreciated. This is a simple join query but I don't know how to construct it with Swift Predicates.

---

<div class="post-metadata">

**Author:** ![jmschonfeld](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jmschonfeld/32/20922_2.png) [@jmschonfeld](https://forums.swift.org/u/jmschonfeld)\
**Post date:** [July 30, 2024, 3:53pm UTC](https://forums.swift.org/t/complex-predicates-in-swiftdata/73565/2 "2024-07-30T15:53:37Z")

</div>

Hey @natebird - you're on the right track! Could you share what your `Quote` type looks like? Presumably, it might have some `author` and `tag` property, right? If so, you'll want to use those relationships in your `filter` to get the author and tag since your `Query` is only filtering over `Quote`s. For example:

```swift
let filter = #Predicate<Quote> {
    contentContainsTerm.evaluate($0) ||
    authorNameContainsTerm.evaluate($0.author) ||
    tagNameContainsTerm.evaluate($0.tag)
}

```

or for a to-many relationship, you could do something like:

```swift
...
$0.authors.contains { author in
    authorNameContainsTerm.evaluate(author)
}
...

```

Does that work for you / get you closer to your goal?

---

<div class="post-metadata">

**Author:** ![natebird](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/natebird/32/596_2.png) [@natebird](https://forums.swift.org/u/natebird)\
**Post date:** [July 30, 2024, 5:21pm UTC](https://forums.swift.org/t/complex-predicates-in-swiftdata/73565/3 "2024-07-30T17:21:26Z")

</div>

Thanks for the guidance @jmschonfeld. I think I'm getting closer but still may be missing something or structuring things incorrectly.

Here is the Quote model

```swift
@Model
public final class Quote {
    public var id: String = UUID().uuidString
    public var content: String = ""
    public var timestamp: Date = Date()
    public var notes: String = ""
    public var source: String = ""
    public var isFavorite: Bool = false
    
    @Relationship(inverse: \Author.quotes) public var authors: [Author]?
    @Relationship(inverse: \Tag.quotes) public var tags: [Tag]?
    ...
}

```

I updated the Query predicate to be something similar to what you demonstrated above.

```swift
let term = searchText.wrappedValue
let contentContainsTerm = #Predicate<Quote> { $0.content.localizedStandardContains(term) }
let authorNameContainsTerm = #Predicate<Author> { $0.name.localizedStandardContains(term) }

let filter = #Predicate<Quote> {
    contentContainsTerm.evaluate($0) ||
    (($0.authors?.contains { author in
          authorNameContainsTerm.evaluate(author) }) != nil)
    }

_quotes = Query(filter: filter, sort: [sortDescriptor])

```

This compiles – although it takes a while and I had to remove the tag relationship because it failed to compile in time - I'm on a M1 16" MBP.

However, after building and running on device the search fails on the first keystroke with the following error. It seems my model isn't set up correctly or CoreData can't handle this to-many relationship.

`CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLFetchRequestContext: 0x3013e3b80> , to-many key not allowed here with userInfo of (null)`

---

<div class="post-metadata">

**Author:** ![natebird](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/natebird/32/596_2.png) [@natebird](https://forums.swift.org/u/natebird)\
**Post date:** [August 7, 2024, 1:32am UTC](https://forums.swift.org/t/complex-predicates-in-swiftdata/73565/4 "2024-08-07T01:32:22Z")

</div>

@jmschonfeld do you have any suggestions to move past this issue?

`CoreData: error: SQLCore dispatchRequest: exception handling request: <NSSQLFetchRequestContext: 0x3013e3b80> , to-many key not allowed here with userInfo of (null)`

---

<div class="post-metadata">

**Author:** ![jmschonfeld](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jmschonfeld/32/20922_2.png) [@jmschonfeld](https://forums.swift.org/u/jmschonfeld)\
**Post date:** [August 7, 2024, 4:28pm UTC](https://forums.swift.org/t/complex-predicates-in-swiftdata/73565/5 "2024-08-07T16:28:01Z")

</div>

Hey sorry for the delay - looking into it a bit this seems like it might be a bug in how SwiftData consumes the `Predicate` rather than in the construction/representation of the `Predicate` itself in Foundation. Would you be able to file a feedback (feel free to post the feedback number here / DM it to me) and we can route that to the right team to investigate further
