核心数据:查询谓词中的objectID? [英] Core Data: Query objectIDs in a predicate?

查看:147
本文介绍了核心数据:查询谓词中的objectID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用抓取请求和谓词从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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆