SwiftData - Problems with Predicates and Relations

Hello,

I have a problem with SwiftData and Predicates that check for the persistentModelID of the relations.

My data model looks simplified like this:

Day -> TimeEntry[] -> Hashtag[]

What I want to achieve is to query the days and associated time entries via assigned tags.

This is my predicate:

       let identifier = filterHashtags.map(\.persistentModelID)
       ...
       #Predicate<TimeEntry> { timeEntry in
            identifiers.count == timeEntry.tags.filter { tag in
                identifiers.contains(tag.persistentModelID)
            }.count
        }

It does not return any data when I check for the persistentModelID. However, if I use another property of the tags, e.g. the name or a generated UUID for the check, the predicate works. Is this a general problem with PersistentIdentifier in Predicates or am I missing something?

Thanks in advance

where does the content of filterHashtags come from?

The hashtags come from my HashtagFilter model and are attached there as a relationship.

    @Model
    final class HashtagFilter: Identifiable, Hashable, NamedEntity {
        @Relationship(inverse: \Hashtag.hashtagFilter)
        var tags: [Hashtag]
        var displayName: String
        
        @Relationship(deleteRule: .cascade, inverse: \HashtagFilter.parentItem)
        var children: [HashtagFilter]?
        var parentItem: HashtagFilter?
                
        @Transient
        var isEditing: Bool = false
        
        init(displayName: String) {
            self.tags = []
            self.children = []
            self.displayName = displayName
        }
    }
    ```

The filterHashtags come from HashtagFilter.tags. If I check for equality in my code, it works. It just doesn't work in the Predicate. However, due to the amount of data, I would like to perform this filtering in the Predicate

But Hashtag and HashtagFilter are two different models so why should their persistentModelID values be equal? Your variable naming confused me.

SwiftData - Problems with Predicates and Relations
 
 
Q