Core Data NSPredicate fetch on entity relationship using in clause

微笑、不失礼 提交于 2020-01-23 05:11:24

问题


I would like to be able to search the relationship of an entity using an IN clause. I have the following setup:

I would like to send over an array of nicknames and find all Persons associated with those 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: 'unimplemented SQL generation for predicate : (ANY Person.nickName IN {"Tom", "Tommy", "Thomas"})'

What am I doing wrong here? If I remove the predicate search, it returns all the results ok, but as soon as I add this line in, everything breaks.


回答1:


You should use the relationship name in your NSPredicate.
Try this:

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


来源:https://stackoverflow.com/questions/28158610/core-data-nspredicate-fetch-on-entity-relationship-using-in-clause

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!