问题描述
我正在使用获取请求和谓词从 Core Data 持久存储中获取一组对象.我当前的谓词只是检查一个属性是否 >= 某个值.这一切都很好,除了我想最终排除当前保存在数组中的任何对象.
I am fetching a set of objects from a Core Data persistent store using a fetch request and a predicate. My current predicate simply checks whether an attribute is >= a certain value. This all works great, except that I want to finally exclude any objects that are currently held in an array.
我基本上需要能够排除一组对象,我认为我能做到这一点的唯一方法是能够从我的托管对象数组中获取 objectID
列表,并且在我的谓词中创建另一个表达式,以确保返回的任何对象都没有相同的 objectID
.即@"ANY records.objectID NOT IN %@", arrayOfObjectID
.
I basically need to be able to exclude a set of objects, and the only way I think I can do this is to be able to get a list of objectID
from my managed objects array, and create another expression in my predicate to ensure that any objects returned don't have the same objectID
. I.E.@"ANY records.objectID NOT IN %@", arrayOfObjectID
.
我该怎么做?
推荐答案
像这样的谓词
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (self IN %@)", arrayOfExcludedObjects];
获取请求的实体是数组中对象的实体,应该做你想做的事.当然,这可以与单个谓词中的其他子句组合用于获取请求.
where the entity of the fetch request is the entity of objects in the array, should do what you want. This can, of course be combined with other clauses in a single predicate for a fetch request.
一般来说,对象比较(例如 self == %@
或 self IN %@
)在 Core Data 查询中比较 objectID
.参数可以是一个 NSManagedObject 实例或一个 NSMangedObjectID
实例.所以上面的谓词格式可以以 arrayOfExcludedObjects
或 [arrayOfExcludedObjects valueForKey:@"objectID"]
作为参数.
In general, object comparisons (e.g self == %@
or self IN %@
) compare on objectID
in Core Data queries. The argument can either be an NSManagedObject instance or an NSMangedObjectID
instance. So the predicate format above could take arrayOfExcludedObjects
or [arrayOfExcludedObjects valueForKey:@"objectID"]
as the argument.
这篇关于核心数据:在谓词中查询 objectID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!