NSPredicate Source Data for Entity Relationships Used in the Article

I would like to be able to search for object relationships using the IN clause. I have the following setup: enter image description here

I would like to send an array of aliases and find all the individuals associated with these nicknames.

//array of names to find let nameArray: [String] = ["Tom", "Tommy", "Thomas"] // find all Persons who have a nickname associated with that person let predicate = NSPredicate(format: "ANY Person.nickName in %@",nameArray) var fetch = NSFetchRequest(entityName: "Person") fetch.predicate = predicate var fetchError : NSError? = nil // executes fetch let results = context?.executeFetchRequest(fetch, error: &fetchError) 

But when I run the code, I get the following error:

'NSInvalidArgumentException', reason: 'unrealized SQL generation for the predicate: (ANY Person.nickName IN {"Tom", "Tommy", "Thomas"})'

What am I doing wrong here? If I delete the predicate search, it will return all the results in order, but as soon as I add this line, everything will break.

+5
source share
1 answer

You must use the name of the relationship in your NSPredicate .
Try the following:

 let predicate = NSPredicate(format: "ANY personToNick.nickName in %@", nameArray) 
+8
source

Source: https://habr.com/ru/post/1211955/


All Articles