核心数据:查询谓词中的objectID? [英] Core Data: Query objectIDs in a predicate?
问题描述
我使用抓取请求和谓词从Core Data持久存储中提取一组对象。我的当前谓词简单地检查属性是否> =某个值。这一切都很好,除了我想最终排除当前在数组中保存的任何对象。
我基本上需要能够排除一组对象,并且我认为我可以这样做的唯一方法是能够从我的托管对象数组中获取 objectID
的列表,并在我的谓词中创建另一个表达式,以确保任何返回的对象不具有相同的 objectID
。 IE @ANY records.objectID NOT IN%@,arrayOfObjectID
。
/ p>
如
的谓词 NSPredicate * predicate = [NSPredicate predicateWithFormat:@NOT(self IN%@),arrayOfExcludedObjects];
其中获取请求的实体是数组中对象的实体, 。这当然可以与用于获取请求的单个谓词中的其他子句组合。
通常,对象比较(例如 self == Core Data查询中的
或 objectID
上的%@ self IN%@
参数可以是NSManagedObject实例 NSMangedObjectID
实例。因此,上面的谓词格式可以使用 arrayOfExcludedObjects
或 [arrayOfExcludedObjects valueForKey:@objectID]
p>
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.
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
.
How can I do this?
A predicate like
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.
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!