核心数据使用in子句对实体关系进行NSPredicate fetch [英] Core Data NSPredicate fetch on entity relationship using in clause
问题描述
我想能够使用IN子句搜索实体的关系。
我有以下设置:
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' '未实现的谓词谓词生成:(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.
推荐答案
p>您应该在 NSPredicate
中使用关系名称。
尝试此操作:
You should use the relationship name in your NSPredicate
.
Try this:
let predicate = NSPredicate(format: "ANY personToNick.nickName in %@", nameArray)
这篇关于核心数据使用in子句对实体关系进行NSPredicate fetch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!