I'm trying to write a NSPredicate to filter my Students based on their tags.
Student CoreData entity has an NSSet of Tags (to many relationship):
extension Student {
@NSManaged public var tags: NSSet?
And Tag CoreData entity has the inverse to-many relationship as well:
extension Tag {
@NSManaged public var students: NSSet?
I use this request to select tags for filtering, based on a query String:
let dataController = DataController.shared
let request: NSFetchRequest<Tag> = Tag.fetchRequest()
request.predicate = NSPredicate(format: "%K CONTAINS[cd] %@", #keyPath(Tag.title), query)
let moc = dataController.container.viewContext
let fetchedTags = try? moc.fetch(request)
I get this way an optional array of tags ([Tag]?), or an empty one. That's working ok.
But, and that is the key point of my question, now I'm trying to use this array of tags to filter which students have that tags, this way:
tagPredicate = NSPredicate(format: "%K IN %@", #keyPath(Student.tags), fetchedTags!)
But it fails, with the error "NSInvalidArgumentException', reason: 'unimplemented SQL generation for predicate".
I'm trying to use different argument sintaxes, as detailed here: https://nspredicate.xyz, but neither "SELF CONTAINS", nor "IN" options don't work.
Does anybody knows how to get it working?
Thanks!